代码之家  ›  专栏  ›  技术社区  ›  xRobot

错误URL重定向

  •  2
  • xRobot  · 技术社区  · 15 年前

    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/

    如果url中有错误,我希望通过在url末尾使用book.id重定向到真正的url。

    例如,如果我去:

    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造成的*

    为什么? 我该怎么办?

    2 回复  |  直到 15 年前
        1
  •  3
  •   Ignacio Vazquez-Abrams    15 年前

    int() %s 相反。

        2
  •  0
  •   mpen    15 年前

    是的,只要换个新的 %i

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

    %s . 不用费心把它转换成整数。