0

I'm creating a blog website and I want it to be that when a user update a post, I want to redirect them to the view page for that article.

the urls.py

url_patterns = [
    path('view/<str:id>/, views.viewPost, name='viewpost'), 
    path('update/<str:id>/, views.updatePost, name='updatepost') 
] 

My problem is that, I want to be able to redirect to the viewpost when I update a post

I tried

return redirect('viewpost' id)

But it was giving me an error

Thanks for your contribution

2
  • What was the error? what did you find when researching that error? Commented Jul 18, 2021 at 18:36
  • The error I got was NoReverseMatch at /update/41 Commented Jul 18, 2021 at 18:50

1 Answer 1

1

You specify this with:

id = 1425  # some id
return redirect('viewpost', id=id)

Note: It might be better to use pk over id, since the pk field of a model always refers to the primary key, regardless the name of the field that is the primary key. Furthermore Python has a builtin id(…) [Python-doc] function, thus by using id you will "shadow" the builtin.

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

2 Comments

Ok, so pk is better than id. Thanks
Alright that worked, thanks for the advice tho

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.