在Django中重定向时如何传递extra_context [英] How to pass extra_context when redirecting in Django

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

问题描述

我的views.py:

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)

在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()
           }

如您所见,我正在使用Django具有两个额外Django表单字段的模型表单,即 AddressCreateForm 类中的 Address和value 。在呈现模板时,我需要所有字段。

As you see that i am using Django 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.

独立 some_views 方法工作正常,但我还希望将以 context_dictionary 编写的一些额外数据呈现给请求 URL user_list c $ c>即 address_create.get_absolute_url()

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().

如果我没有记错,如果我们处理数据库,我们必须使用 redirect_to 方法。

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?

推荐答案

重定向将返回状态代码为301或302以及位置为重定向到:

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/

原始视图没有呈现模板,因此您无法通过 extra_context

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

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

The user's 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. 设计您的URL模式以包括用户ID,例如 / users / 200 /

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

  3. 将用户名存储在会话中

  4. 重定向之前,请使用消息框架使用用户数据。

  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 from request.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.

这篇关于在Django中重定向时如何传递extra_context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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