Hi!
Hope everyone is doing well. Appreciate any help I can get with the following:
When uploading file by opening the file_upload.html template directly which contains the html form the server gives the following message:
[13/Nov/2022 08:00:54] “POST /upload_view/ HTTP/1.1” 200 948 and i see the file in the media root folder.
When doing the same thing but opening up index.html which uses {% include %} to bake in the file_upload.html I see this:
[13/Nov/2022 08:02:59] “POST / HTTP/1.1” 200 1538 and no uploaded file in media root folder.
My views:
def index_view(request, *args, **kwargs):
return render(request, "index.html", {})
def upload_view(request, *args, **kwargs):
if request.method == 'POST':
user_file = request.FILES['filename']
stored_file = FileSystemStorage()
stored_file.save(user_file.name, user_file )
return render(request, "file_upload.html", {})
my templates:
index.html
{% extends "base_template.html" %}
{% block content %}
<p>this is a test</p>
{% include "file_upload.html" %}
{% endblock %}
file_upload.html
{% extends "base_template.html" %}
{% block content %}
<p>Upload</p>
<form method="POST" enctype="multipart/form-data">
<!--cross site request forgery documentation https://docs.djangoproject.com/en/4.1/ref/csrf/-->
{% csrf_token %}
<input type="file" name="filename">
<button type="submit"> Upload file</button>
</form>
{% endblock %}
url patterns:
urlpatterns = [
path('admin/', admin.site.urls),
path('', index_view, name='index'),
path('index/', index_view, name='index'),
path('upload_view/', upload_view, name='fileupload'),
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('static/favicon/favicon.ico')))