Error URL redirection
Posted
by xRobot
on Stack Overflow
See other posts from Stack Overflow
or by xRobot
Published on 2010-05-09T00:37:13Z
Indexed on
2010/05/09
0:38 UTC
Read the original article
Hit count: 595
django
|django-urls
urls.py:
url(r'^book/(?P<booktitle>[\w\._-]+)/(?P<bookeditor>[\w\._-]+)/(?P<bookpages>[\w\._-]+)/(?P<bookid>[\d\._-]+)/$', 'book.views.book', name="book"),
views.py:
def book(request, booktitle, bookeditor, bookpages, bookid, template_name="book.html"):
book = get_object_or_404(book, pk=bookid)
if booktitle != book.book_title :
redirect_to = "/book/%s/%s/%s/%s/%i/" % ( booktitle, bookeditor, bookpages, bookid, )
return HttpResponseRedirect(redirect_to)
return render_to_response(template_name, { 'book': book, },)
.
So the urls of each book are like this:
example.com/book/the-bible/gesu-crist/938/12/
I want that if there is an error in the url, then I get redirected to the real url by using book.id in the end of the url.
For example if I go to:
example.com/book/A-bible/gesu-crist/938/12/
the I will get redirected to:
example.com/book/the-bible/gesu-crist/938/12/
but I go to wrong url I will get this error:
TypeError at /book/A-bible/gesu-crist/938/12/
%d format: a number is required, not unicode
.
Why ? What I have to do ?
© Stack Overflow or respective owner