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

查看:38
本文介绍了在 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 中写入的一些额外数据(即 user_list)渲染到请求的 URLaddress_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 的 HTTP 响应,以及要重定向到的位置:

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. 在会话中存储 user_id
  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天全站免登陆