Django Templates, NameError error question.

Asked 2 years ago, Updated 2 years ago, 53 views

I'm running a Jango Girls tutorial but it's not working well

First of all, this is the error.

NameError at / name 'posts ' is not defined

#mysite/views.py

from django.shortcuts import render
from django.utils import timezone
from .models import Post

# # Create your views here.

def post_list(request):
    Posts = Post.objects.filter(pub_date__lte = timezone.now()).order_by('pub_date')
    return render(request, 'mysite/post_list.html', {'posts':posts})

For your information, mysite is an application...

The following are template files...

  <html>
        <head>
            <title>Django Girls blog</title>
        </head>

        <body>

            <div>
                <h1><a href="">Django Girls Blog</a></h1>
            </div>

    {% {% for post in posts %}
            <div>
                <p>published: {{ post.pub_date }}</p>
                <h1><a href="">{{ post.product }}</a></h1>
                <p>{{ post.description|linebreaksbr }}</p>
            </div>
        {% {% endfor %}

        </body>
    </html>

django python

2022-09-22 21:40

2 Answers

I don't know if it's a simple typo or a different problem, but if you look at the code,

def post_list(request):
    Posts = Post.objects.filter(pub_date__lte = timezone.now()).order_by('pub_date')
    return render(request, 'mysite/post_list.html', {'posts':posts})
    # Posts, not posts

I think this is a problem.

You wrote the Posts variable incorrectly as posts


2022-09-22 21:40

Thank you so much for your answers Haha


2022-09-22 21:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.