0

My current url pattern is:

url(r'^(?P<category>\w+)/(?P<hash>\w+)/$', article, name='article'),

hash is an alphanumeric string that can be uppercase or lowercase.

example: gqaBittXW9hcyO

What should my url pattern look like to operate for this?

Error:

NoReverseMatch at /news/gqaBittXW9hcyO/
Reverse for 'article' with keyword arguments '{'id': 1, 'category': 'news'}' not found. 1 pattern(s) tried: ['(?P<category>\\w+)/(?P<hash>\\w+)/$']

views

def article(request, category, hash, extra_context=None):

    name = resolve(request.path).kwargs['category']
    instance = get_object_or_404(Post, hash=hash, entered_category=name)
    print('Hash:', instance.hash) #prints correctly (gqaBittXW9hcyO)

    context = {
        'id': instance.id,
        'instance': instance,
    }

    return render(request, 'article.html', context)
1
  • Current pattern should work fine. What is your problem? Commented Mar 24, 2018 at 11:21

1 Answer 1

1

The error indicates that somewhere (probably your template) you tried to create a URL with the name article with arguments {'id': 1, 'category': 'news'}. But the way you've defined it, you can't construct an article URL using an id, you need a hash.

The URL /news/gqaBittXW9hcyO/ is not related to this error, it matched correctly.

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

1 Comment

yes you're right, there was a URL with the name article with arguments {'id': 1, 'category': 'news'} hiding somewhere in the template. All fixed now, thanks.

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.