Django返回带有参数的重定向() [英] Django return redirect() with parameters

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

问题描述

在我的视图函数中,我想调用另一个视图并将数据传递给它:

In my view function I want to call another view and pass data to it :

return redirect('some-view-name', backend, form.cleaned_data)

,其中 backend 是 registration.backends 对象,form.cleaned_data 是表单数据的字典(但两者都必须作为 *args 或 **kwargs 发送以防止提高 不要混合 *args 和**kwargs 调用 reverse()! 错误).从我在文档中找到的内容:

, where backend is of registration.backends object, and form.cleaned_data is a dict of form data (but both must be either sent as *args or **kwargs to prevent raising Don't mix *args and **kwargs in call to reverse()! error). From what I've found in the docs :

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

看起来我需要提供some-view-name"参数,但它只是视图函数的名称,还是 url 的名称?所以我想让它类似于在 django-registration 中完成的方式,其中:

It looks like I need to provide 'some-view-name' argument, but is it just the name of the view function, or the name of the url ? So I would like to make it similar to the way it's done in django-registration, where :

to, args, kwargs = backend.post_registration_redirect(request, new_user)
return redirect(to, *args, **kwargs)

def post_registration_redirect(self, request, user):
    return ('registration_complete', (), {})

好的,现在我可以直接调用我的视图函数还是需要为它提供一个 url?更重要的是,我的 funciotn 调用(以及 url,如果需要)应该是什么样子?backend 和cleaned_data 都只是通过此视图传递以供以后使用.我试过这个,但这是不正确的:

Ok so now, can I call directly my view function or do I need to provide a url for it ? And what more important, how my funciotn call (and a url if needed) should look like ? Both backend, and cleaned_data are just passed through this view for a later usage. I've tried this, but it's improper :

url(r'^link/$', some-view-name)   
def some-view-name(request, *args):

还有这个:

return redirect('some_url', backend=backend, dataform.cleaned_data) 
url(r'^link/$', some-view-name)    
def some-view-name(request, backend, data):

仍然是 NoReverseMatch .但是在 django-registration 中,我看到过这样的事情:

still NoReverseMatch . But in django-registration, I've seen something like this :

url(r'^register/$',register,{'backend': 'registration.backends.default.DefaultBackend'}, name='registration_register'),

def register(request, backend, success_url=None, form_class=None,
             disallowed_url='registration_disallowed',
             template_name='user/login_logout_register/registration_form.html',
             extra_context=None):

推荐答案

首先,您的 URL 定义根本不接受任何参数.如果你想让参数从 URL 传递到视图中,你需要在 urlconf 中定义它们.

Firstly, your URL definition does not accept any parameters at all. If you want parameters to be passed from the URL into the view, you need to define them in the urlconf.

其次,完全不清楚您期望对cleaned_data 字典发生什么.不要忘记你不能重定向到 POST - 这是 HTTP 的限制,而不是 Django - 所以你的 clean_data 要么需要是一个 URL 参数(可怕的),或者更好一点,一系列 GET 参数 - 所以 URL将采用以下形式:

Secondly, it's not at all clear what you are expecting to happen to the cleaned_data dictionary. Don't forget you can't redirect to a POST - this is a limitation of HTTP, not Django - so your cleaned_data either needs to be a URL parameter (horrible) or, slightly better, a series of GET parameters - so the URL would be in the form:

/link/mybackend/?field1=value1&field2=value2&field3=value3

等等.在这种情况下,field1、field2 和 field3 包含在 URLconf 定义中 - 它们可以通过 request.GET 在视图中使用.

and so on. In this case, field1, field2 and field3 are not included in the URLconf definition - they are available in the view via request.GET.

所以你的 urlconf 将是:

So your urlconf would be:

url(r'^link/(?P<backend>w+?)/$', my_function)

视图将如下所示:

def my_function(request, backend):
   data = request.GET

反之(导入urllib后):

return "%s?%s" % (redirect('my_function', args=(backend,)),
                  urllib.urlencode(form.cleaned_data))

评论后编辑

正如您一直在做的那样,使用重定向和反向的全部意义在于您转到 URL - 它返回一个 Http 代码,导致浏览器重定向到新的 URL,并调用它.

The whole point of using redirect and reverse, as you have been doing, is that you go to the URL - it returns an Http code that causes the browser to redirect to the new URL, and call that.

如果您只是想从代码中调用视图,只需直接调用 - 根本不需要使用 reverse.

If you simply want to call the view from within your code, just do it directly - no need to use reverse at all.

也就是说,如果您只想存储数据,那么只需将其放入会话中即可:

That said, if all you want to do is store the data, then just put it in the session:

request.session['temp_data'] = form.cleaned_data

这篇关于Django返回带有参数的重定向()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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