Django使用参数返回redirect() [英] Django return redirect() with parameters

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

问题描述

在我的查看功能中,我想调用另一个视图并传递数据:

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

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

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

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

  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应该如何?后端和clean_data都刚刚通过此视图以供以后使用。我已经尝试过这个,但这是不正确的:

  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(请求,后端,数据):

still NoReverseMatch。但是在django注册中,我看到过这样的东西:

  url(r'^ 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 =无):


解决方案

首先,您的URL定义根本不接受任何参数。如果您希望将参数从URL传递到视图中,则需要在urlconf中定义参数。



其次,您不太清楚您期望的内容发生在cleaning_data字典。不要忘记你不能重定向到POST - 这是HTTP的限制,而不是Django - 所以你的clean_data要么需要一个URL参数(可怕的),要么稍微更好一些GET参数,所以URL将以以下形式:

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

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

所以你的urlconf将是:

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

看起来像:

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

,反之亦然(导入 urllib )后:

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

评论后编辑



正如您所做的一样,使用重定向和反向的全部要点是您访问该URL - 它返回一个Http代码,导致浏览器重定向到新的URL ,并调用。



如果你简单的话你想从您的代码中调用视图,只需直接执行 - 不需要使用反向。



说,如果你想做的是存储数据,然后把它放在会话中:

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


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

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

, 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')

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', (), {})

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):

As well as this :

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

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):

解决方案

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.

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

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.

So your urlconf would be:

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

and the view would look like:

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

and the reverse would be (after importing urllib):

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

Edited after comment

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.

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使用参数返回redirect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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