错误 URL 重定向
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
传递给视图的所有参数都是字符串。在使用之前将其推入
int()
中,或者直接使用%s
代替。All arguments passed to a view are strings. Shove it through
int()
before using it, or just use%s
instead.是的,只需将
%i
替换为
%s
即可。不要费心将其转换为整数。Yeah, just replace the
%i
inWith
%s
. Don't bother casting it to an integer.