I keep getting this error message:
Reverse for 'tagged' with arguments '('',)' not found. 1 pattern(s) tried: ['\^tag/\(\?P(?P[^/]+)\[\-\\w\]\+\)/\$$']
when trying to load my base.html, because of an anchor tag. I have looked at other posts with the same issue, but I still don't know where I'm going wrong.
views.py
class TagIndexView(TagMixin, ListView):
template_name = 'post/index.html'
model = Post
paginate_by = '10'
context_object_name = 'posts'
def get_queryset(self):
return Post.objects.filter(tags__slug=self.kwargs.get('slug'))
def tagged(request):
return render(request, 'blog/tagged.html', {'title': 'Tagged'})
urls.py
path(r'^tag/(?P<slug>[-\w]+)/$',TagIndexView.as_view, name='tagged')
base.html
The anchor tag:
<li class="list-group-item"><a href="{% url 'tagged' tag.slug %}">Tags</a></li>
But I keep getting a NoReverseMatch. In the anchor tag I have tried "tag.slug", "tag.id", "tag.pk" and some others.
Use re_path() instead of path() for regular expressions.
Reference: https://docs.djangoproject.com/en/3.2/topics/http/urls/#using-regular-expressions
Use [-\w]* (0 or more) instead of [-\w]+ (1 or more) to allow empty string ('',).
Call .as_view with .as_view() to create the view function.
# path(r'^tag/(?P<slug>[-\w]+)/$', TagIndexView.as_view, name='tagged')
re_path(r'^tag/(?P<slug>[-\w]*)/$', TagIndexView.as_view(), name='tagged')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With