如何将extra_context传递给“ redirect_to“方法在Django python [英] How to pass extra_context to " redirect_to " method in Django python

查看:363
本文介绍了如何将extra_context传递给“ redirect_to“方法在Django python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的views.py:

  @login_required 
def some_views(request):
如果请求.method =='POST':
form = AddressCreateFrom(request.POST)
如果form.is_valid():
name = form.cleaned_data ['Address']
ip_value = form.cleaned_data ['value']
user_list = get_username(name)
address_create = form.save()
extra_context = {
'user_list':user_list
}
return redirect_to(request,url = address_create.get_absolute_url())
else:
form = AddressCreateFrom()
extra_context = {
'form':AddressCreateFrom initial = {'user':request.user.pk}
}
return direct_to_template(request,'networks / user_form.html',extra_context)
/ pre>

在form.py:

  class AddressCreateFrom orms.ModelForm):
Address = forms.CharField(max_length = 40)
value = forms.CharField(max_length = 40)
class Meta:
model = Network
widgets = {
'user':forms.HiddenInput()
}

你看到我正在使用Django表单与Django两个表单字段,即地址和值 AddressCreateForm 类中。在渲染模板时,我需要所有的字段。



确实 some_views 方法工作正常,但我还要在 context_dictionary 中写入一些额外的数据,即 user_list 到请求的 URL ie address_create.get_absolute_url()



如果我没有错,如果我们处理数据库我们必须使用 redirect_to 方法。是否可以这样做?

解决方案

重定向将返回HTTP响应,状态码为301或302,位置为重定向到:

  301 MOVED PERMANENTLY 
位置:http://www.example.com/new-url/

原始视图没有渲染模板,因此您不能通过 extra_context



用户浏览器通常会遵循重定向,并请求新的URL。



如果要在下一个视图中显示有关特定用户的信息,您必须执行以下操作:


  1. 设计您的网址模式以包含用户ID,例如 / users / 200 /

  2. 将其包含为get参数 / users /?id = 200,然后从视图中的 request.GET`中获取用户ID。

  3. 在会话中存储user_id

  4. 在重定向之前,使用消息框架

然后在您重定向到的视图中,您可以从数据库中获取用户,并将其添加到模板上下文。


My views.py:

 @login_required
 def some_views(request):
     if request.method == 'POST':
         form = AddressCreateFrom(request.POST)
         if form.is_valid():
             name = form.cleaned_data['Address']
             ip_value = form.cleaned_data['value']
             user_list = get_username(name)
             address_create = form.save()
             extra_context = {
                 'user_list': user_list
                 }
             return redirect_to(request, url=address_create.get_absolute_url())
     else:
         form = AddressCreateFrom()
     extra_context = {
         'form':AddressCreateFrom(initial={'user': request.user.pk})
         }
     return direct_to_template(request,'networks/user_form.html',extra_context)

In form.py:

 class AddressCreateFrom(forms.ModelForm):
     Address = forms.CharField(max_length=40)
     value = forms.CharField(max_length=40)
     class Meta:
         model = Network
         widgets = {
             'user': forms.HiddenInput()
           }

As you see that i am using Djang model form with two extra Django form field i.e. Address and value in AddressCreateForm class. I need all of the field at the time of rendering the template.

Indeed some_views method are working fine but i also want render some extra data written in context_dictionary i.e. user_list to a requesting URL i.e. address_create.get_absolute_url().

If i am not wrong, if we are handling with the database we have to use redirect_to method. Is it possible to do that?

解决方案

A redirect will return a HTTP response with status code 301 or 302, and the location to redirect to:

301 MOVED PERMANENTLY
Location: http://www.example.com/new-url/

There is no template rendered by the original view, so you can't pass extra_context to it.

The users browser will usually follow the redirect, and request the new url.

If you want to display information about a particular user in the next view, you have to do something like:

  1. design your url pattern to include the user id, e.g. /users/200/,
  2. include it as a get parameter e.g. /users/?id=200, then fetch the user id fromrequest.GET` in the view.
  3. store the user_id in the session
  4. Before redirecting, create a message using the messages framework using the user data.

Then in the view that you redirect to, you can fetch the user from the database, and add it to the template context.

这篇关于如何将extra_context传递给“ redirect_to“方法在Django python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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