Error URL redirection

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 for each book look like this:

example.com/book/the-bible/gesu-crist/938/12/

I want that if there is an error in the url I redirect to the real url using book.id at the end of the url.

For example, if I go to:

example.com/book/A-bible/gesu-crist/938/12/

then I redirect to:

example.com/book/the-bible/gesu-crist/938/12/

...

but if i go to the wrong url i get this error:

TypeError at /book/A-bible/gesu-crist/938/12/

%d format: a number is required, not unicode

      

...

If I use% s then I get this error:

* The page is not redirecting as expected. Firefox discovered that the server redirects the request to this address in a way that will never complete. * Sometimes this problem can be caused by disabling or refusing to accept cookies. *

Why? What should I do?

+2


a source to share


2 answers


All arguments passed to the view are strings. Drag and drop it before using it int()

or just use it %s

.



+3


a source


Yes, just replace %i

with

redirect_to = "/book/%s/%s/%s/%s/%i/" % ( booktitle, bookeditor, bookpages, bookid, )

      



With %s

. Do not bother distinguishing it with an integer.

0


a source







All Articles