错误 URL 重定向

发布于 2024-08-31 19:38:00 字数 1236 浏览 6 评论 0原文

urls.py:

url(r'^book/(?P<booktitle>[\w\._-]+)/(?P<bookeditor>[\w\._-]+)/(?P<bookpages>[\w\._-]+)/(?P<bookid>[\d\._-]+)/$', 'book.views.book', name="book"), 

视图.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, },)

.

所以每本书的网址都是这样的:

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

我希望如果网址中有错误,那么我可以使用以下命令重定向到真实的网址网址末尾的 book.id。

例如,如果我访问:

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

,那么我将被重定向到:

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

但如果我访问错误的网址,我会收到此错误:

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

%d format: a number is required, not unicode

如果我使用 %s,则会收到此错误:

*页面未正确重定向 Firefox 已检测到服务器正在以永远无法完成的方式重定向对此地址的请求。 * 此问题有时可能是由于禁用或拒绝接受 cookie 引起的。*

为什么? 我必须做什么?

urls.py:

url(r'^book/(?P<booktitle>[\w\._-]+)/(?P<bookeditor>[\w\._-]+)/(?P<bookpages>[\w\._-]+)/(?P<bookid>[\d\._-]+)/

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/

then I will get redirected to:

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

.

but if 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

.

If I use %s then I will get this error:

*The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. * This problem can sometimes be caused by disabling or refusing to accept cookies.*

Why ?
What I have to do ?

, 'book.views.book', name="book"),

views.py:

.

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/

then I will get redirected to:

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

.

but if I go to wrong url I will get this error:

.

If I use %s then I will get this error:

*The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. * This problem can sometimes be caused by disabling or refusing to accept cookies.*

Why ?
What I have to do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野鹿林 2024-09-07 19:38:00

传递给视图的所有参数都是字符串。在使用之前将其推入 int() 中,或者直接使用 %s 代替。

All arguments passed to a view are strings. Shove it through int() before using it, or just use %s instead.

污味仙女 2024-09-07 19:38:00

是的,只需将 %i 替换

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

%s 即可。不要费心将其转换为整数。

Yeah, just replace the %i in

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

With %s. Don't bother casting it to an integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文