0

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']

4
  • slugs don't have underscores... Commented Jul 14, 2023 at 20:14
  • @WillemVOsupportsmodstrike as per Django documentation it should handle underscores. Problem could be non-ASCII characters. Commented Jul 14, 2023 at 20:40
  • It might just be a problem with single versus double quotes. Try changing your <a href="{% url "main:task_list_by_tag" tag.slug %}"> to this: <a href="{% url 'main:task_list_by_tag' tag.slug %}"> Commented Jul 14, 2023 at 21:20
  • @Marco: aargghh, I didn't see that it was cyrillic. No that will indeed not work, the slug only accepts ASCII alphanumeric characters. Commented Jul 14, 2023 at 22:23

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

your answer is correct. it helped me. thank u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.