Home > OS >  Need help about redirecting views in Django
Need help about redirecting views in Django

Time:12-21

I a beginner in learning Django and have met with an issue that need everyone help.

I'm working on a web application for reading novels. I met with an issue that is related to views redirecting in Django. It's about how I can open the chapter page that I clicked on and read it perfectly normal but when I want to return to the novel index page by clicking on the link, I'm met with a NoReverseMatch error. I'll post my code below:

This is what I configured in the urls.py:

Then I created and configured the urls.py like this:

And this is my views.py:

The part that I've met with issue is an Html page for viewing chapter:

What I have in mind is that by clicking on the url, I'll be redirected to the novel index page. But what happened is that I'm met with the NoReveseMatch error because the part that should have the slug that point to the index page is blank so I can't redirect to it.

Anyone have an idea of what I did wrong ? Any helps or suggestions are appreciated

CodePudding user response:

You need to correction of url

You can pass url in django html like this...

href='{% 'book' book.slug %}'

-------- OR ---------

href='book/book.slug/'

CodePudding user response:

The url templatetag takes the name of the url (i.e. the value you pass to the name parameter when you define the path) as its first argument. If the url path itself requires args, those then get included in the url templatetag after the name.

It looks like you're trying to link to the first path that's named 'book_detail'? If that's the case, you would use {% url 'book_detail' book.slug %}.

Read the url templatetag docs for a better understanding of how it works.

Also, you have two different paths both with the name 'book_detail'. Path names should be unique within a single app.

Finally, you should put your code directly into the body of your question rather than including screenshots.

  • Related