Error: Reverse for 'task_list_by_tag' with arguments '('оаип_1',)' not found. 1 pattern(s) tried: ['tag/(?P<tag_slug>[-a-zA-Z0-9_]+)/\Z']
views.py
def index(request, tag_slug=None):
task_list = Task.objects.all()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
task_list = task_list.filter(tags__in=[tag])
paginator = Paginator(task_list, 2)
page_number = request.GET.get('page', 1)
try:
tasks = paginator.page(page_number)
context = {"tasks":tasks, 'tag':tag}
return render(request, 'main/index.html', context)
urls.py
app_name = 'main'
...
path('tag/<slug:tag_slug>/', views.index, name = "task_list_by_tag"),
index.html
{% for tag in task.tags.all %}
<a href="{% url "main:task_list_by_tag" tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
I tried to fix that problem:
1)
<a href="{% url "main:task_list_by_tag" tag_slug=tag.slug %}">
return the error: Reverse for 'task_list_by_tag' with keyword arguments '{'tag_slug': 'оаип_1'}' not found. 1 pattern(s) tried: ['tag/(?P<tag_slug>[-a-zA-Z0-9_]+)/\Z']
2)
`<a href="{% url "main:task_list_by_tag" slug=tag.slug %}">`
Reverse for 'task_list_by_tag' with keyword arguments '{'slug': 'оаип_1'}' not found. 1 pattern(s) tried: ['tag/(?P<tag_slug>[-a-zA-Z0-9_]+)/\Z']
Using <a href="{% url "main:task_list_by_tag" tag.slug %}">
is perfectly fine as Django has found the correct URL mentioned in:
Error: Reverse for 'task_list_by_tag' with arguments '('оаип_1',)' not found. 1 pattern(s) tried: ['tag/(?P<tag_slug>[-a-zA-Z0-9_]+)/\Z']
But as per Django documentation, you can only use "ASCII letters or numbers, plus the hyphen and underscore characters" for slugs:
slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
Make sure that your slug consists only of these characters and not Cyrillic, as you are doing now. Otherwise, change your URL dispatcher to str
.