Django使用kwarg重定向 [英] Django redirect with kwarg

查看:93
本文介绍了Django使用kwarg重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python和django的新手,并且对重定向功能有疑问。

I am new to python and django and i have a question regarding the redirect function.

这是我的views.py文件的简化版本。

This is a reduced version of my views.py file.

def page_index(request, error_message=''):
    print error_message

def add_page(request):
    return redirect('page_index') # this work fine
    return redirect('page_index', error_message='test') # this does not work

这是我的urls.py

And here is a short version of my urls.py

urlpatterns = patterns(
    'x.views',
    url(r'^$', 'page_index', {'error_message': 't'}, name='page_index'),
    url(r'^add/$', 'add_page', name='add_page'),
)

当我尝试在没有关键字参数的情况下重定向到page_index时,一切工作正常,但是当我使用kwag时,出现以下错误消息:

When i try redirecting to page_index without the keyword argument everything works fine, but when i use the kwag i get the following error message:


NoReverseMatch在/ pages / add /

NoReverseMatch at /pages/add/

带有参数()和关键字参数的 page_index的反向找不到'{'error_message':'test'}'。

Reverse for 'page_index' with arguments '()' and keyword arguments '{'error_message': 'test'}' not found.

我在做什么错了?

推荐答案

简短的答案:'error_message'关键字在您的url方案中没有位置。

Short answer: There is no place in your url scheme for the 'error_message' keyword.

更长的答案:

redirect()函数正在调用reverse()来构建URL;它将通过返回带有302重定向状态代码的HTTP响应以及新的URL,将用户的浏览器发送到该URL。您提供给reverse()的所有关键字参数都应该作为url的一部分结束-这就是它们与用户进行交流的方式。

The redirect() function is calling reverse() to build a URL; it is going to send the user's browser to that URL by returning an HTTP response with a 302 redirect status code, and the new url. Any keyword arguments that you supply to reverse() are supposed to end up as part of the url -- that's how they get communicated to the user.

不过, page_index的网址只是定义为 ^ $-这是根网址,在浏览器中看起来像 http://yoursite.com/。

In your case, though, the url for 'page_index` is just defined as '^$' -- this is the root url, which looks like 'http://yoursite.com/' in the browser.

如果您希望能够发出包含其他信息的重定向,则需要在URL中为其定义一个位置,或以其他方式添加它。

If you want to be able to issue a redirect that contains other information, you will need to define a place for it in the url, or add it in a different way.

两种这三种方法很常见:


  1. 使用查询参数-这将消息显式发送到客户端;如果您不小心,人们可以制作网址以使您的索引页说出他们想要的内容。

  1. Use a query parameter -- this sends the message to the client explicitly; if you aren't careful, people can craft urls to make your index page say whatever they want it to.

return redirect(reverse('page-index')+"?error_message=test"))


  • 隐藏会话中的消息并在下一页加载时将其拉出-这要求您配置了会话,并跟踪服务器端的所有内容,而不是依靠客户端向您发送错误消息:

  • Stash the message in the session and pull it out when the next page loads -- this requires that you have sessions configured, and keeps track of everything on the server side, rather than relying on the client to send you back the error message:

    def add_page(request):
        request.session['error_message'] = 'test'
        return redirect('page-index')
    
    def page_index(request):
        print request.session.get('error_message','')
    


  • 为此使用消息框架-只要您不需要太多在同一页面上的消息类型。但是,如果您的模板中只有一个空格用于显示错误消息,那么这确实很容易:

  • Use the messages framework for this -- this is preferred over ad-hoc session attributes, as long as you don't need too many 'types' of message on the same page. If all you have is a space in your template for error message, though, then this is really easy:

    from django.contrib.messages import error
    
    def add_page(request):
        error(request, 'test')
        return redirect('page-index')
    

    然后在您的基本模板中,在某处放置一个这样的块(可能比这更复杂;甚至设置样式):

    And then in your base template, have a block like this somewhere (probably more complex than this; styled, even):

    {% for message in messages %}
        <p>{{ message }}</p>
    {% endfor %}
    


  • 但是,在所有情况下,您都可以从urls.py中删除参数-消息本身不会成为URL路径组成部分。

    In bothall cases, though, you can remove the arguments from your urls.py -- the message itself is not going to be part of the path component of the URL.

     urlpatterns = patterns(
        'x.views',
         url(r'^$', 'page_index', name='page_index'),
         url(r'^add/$', 'add_page', name='add_page'),
     )
    

    这篇关于Django使用kwarg重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆