upload form not uploading files when {{% include % }} is used

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')))

The first issue that jumps out at me is that you have an extends tag in your file_upload.html. The purpose of the include tag is to include the contents of a template into that specific location within the currently-rendered template, which is already extending your base.

(I don’t think your block tag in that included template is doing you any good there either, but I’m not entirely sure about that.)

Hi @KenWhitesell, thank you. I’ve tried removing the extends and the block tag or even just removing the extens inside file_upload.html but the results are exactly the same as leaving everything in there.

First, guaranteed, the extends must not be in the file_upload.html - everything after this point is going to assume that that line has been removed.

Also, I know it will work without the block tags, but since I’m not sure that they will cause a problem, I’m not going to worry about them at the moment.

So for clarity, in your index_view, are you seeing the "this is a test" line?

What is in your base_template.html content block?

  • extends is gone
  • I can confirm I see “this is a test” when opening up index_view.
<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="../static/css/style.css">
    <link rel="apple-touch-icon" sizes="180x180" href="../static/favicon/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="../static/favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="../static/favicon/favicon-16x16.png">
    <link rel="manifest" href="../static/favicon/site.webmanifest">
    <meta charset="UTF-8">
    <title>CSV2PPT</title>
</head>

<body>
    {%  block content %}
    {%  endblock %}
</body>
</html>

I think i’ve figured it out, i got it to upload from both templates.

I changed my index_view to the following:

def index_view(request, *args, **kwargs):
    upload_view(request)
    return render(request, "index.html", {})

That’s a wrong (and possibly bad) solution.

You’re calling upload_view, but throwing away any useful data you may be generating from it (e.g. error messages). Then you’re returning the generic template.

At most, what you would want to do is:

def index_view(request, *args, **kwargs):
    return upload_view(request)

Otherwise, you’ll want to add the logic of that view into this one, to ensure that you can catch and report any errors that may occur.

I also see that I was misreading your original problem description. I had misinterpreted what the problem was.

If I use the code as you have written it then it wouldn’t generate the rest of the page which is something that I would need. Instead of putting all of the code under a single view would something along the lines of the code below not be a solution? I’m trying to keep different pieces under separate views.

def index_view(request, *args, **kwargs):
    upload = upload_view(request)
    return render(request, "index.html", {})

def upload_view(request, *args, **kwargs):
    try:
        if request.method == 'POST':
            user_file = request.FILES['filename']
            stored_file = FileSystemStorage()
            stored_file.save(user_file, user_file)

    except Exception.args as e:
        logging.basicConfig(filename='fileupload.log', encoding='utf-8', level=logging.DEBUG)
        logging.warning(e)

    finally:
        return render(request, "file_upload.html", {})

This doesn’t change anything. You’re still ignoring what you’re getting back from the view.

What I was really aiming toward was this: