Reputation: 39
I am trying to create a view that filters blog posts by topic, so I added a field in my post form called "topic" and,
I created a view: And I defined the path in urls.py:
I get this error in Terminal:
class PoliticalPostListView(ListView):
model = Post
template_name = 'blog/political_posts.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
paginate_by = 5
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(topic=Political).order_by('date_posted')
path('politics/', PoliticalPostListView.as_view(), name='political-posts'),
I used a template from a working user_posts.html to test:
{% extends "blog/base.html" %}
{% block content %}
<h1 class="md-3"> Posts by {{ view.kwargs.username }} ({{ page_obj.paginator.count }})</h1>
{% for post in posts %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{post.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
<small class="text-muted">{{ post.topic }}</small>
</div>
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
<p class="article-content">{{ post.content }}</p>
</div>
</article>
{% endfor %}
{% endblock content %}
Error:
File "/Users/leaghbranner/Desktop/django_project/blog/urls.py", line 21, in <module>
path('politics/', PoliticalPostListView.as_view(), name='political-posts'),
NameError: name 'PoliticalPostListView' is not defined
And the "This Site Cannot Be Reached." page on my browser.
Any thoughts?
Upvotes: 2
Views: 5849
Reputation: 449
In your urls.py
.
if import is used like this:
from . import views
# then use:
path('politics/', views.PoliticalPostListView.as_view(), name='political-posts'),
But if using:
from .views import PoliticalPostListView
# then use:
path('politics/', PoliticalPostListView.as_view(), name='political-posts'),
Upvotes: 1