can not render views in a django template

views.py

In views.py change:

return render("zoo/base.html", {"drivers": drivers})

to:

return render(request, "zoo/base.html", {"drivers": drivers})

template

You need to close your for loop in your template. There’s good information on this worth reading at https://docs.djangoproject.com/en/3.1/ref/templates/builtins/.

{% for driver in drivers %}
    {{ driver.name }}
{% endfor %}

Also I’m guessing from the code you provided and your explanation that {{ drivers.name }} probably won’t render. It won’t be until you iterate through your drivers QuerySet that you’ll be able to access the individual driver fields (like driver.name).

File Structure

It’s best practice to namespace your templates and staticfiles. Django’s tutorial talks about this at https://docs.djangoproject.com/en/3.1/intro/tutorial03/. If you do this your directory structure will change from this (what you currently have):

Project 
      app
         setting.py
         __init__.py
          ...
      zoo
         templates
              base.html
         __init__.py
         models.py
         views.py
          ...

to this:

Project 
      app
         setting.py
         __init__.py
          ...
      zoo
         templates
            zoo
               base.html
         __init__.py
         models.py
         views.py
          ...

Lastly, I strongly recommend you rename your template to something specific to this particular view. If this is your index view, for example, you could name it index.html. Or perhaps zoo.html to correspond with the name of your view.

This isn’t necessary but it will be useful down the road as you build this app out if you want extend a base.html file. This is handy because it allows you to cut down on your boilerplate html and just focus on the core functionality of each template/view. It also has the advantage of making it easier to provide a consistent user experience between different pages that all extend the same base.html file.

To learn more about this check out https://docs.djangoproject.com/en/3.1/ref/templates/language/.

Important Note: If you do this, don’t forget to change your call to render in views.py to accomodate your renamed template file.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top