如何使用parse_qs从视图重定向到另一个? [英] How to use parse_qs in redirecting from view to another?

查看:57
本文介绍了如何使用parse_qs从视图重定向到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一所房子,里面有一张可以从学生那里获得一些信息的表格,以建议他们一些可以应用的程序.主视图如下:

I have a home in which I have a form that I get some info from students to suggest them some programs to apply to. The home view is as below:

def home(request):
    template_name = 'home.html'
    home_context = {}
    if request.POST:
        my_form = MyModelForm(request.POST)
        if my_form.is_valid():
            #  do some stuff
            return programs(request)
    else:
        my_form = MyModelForm()
    home_context.update({'my_form': my_form, })
    return render(request, template_name, home_context)

在第二个视图中,我具有相同的表单,并且希望该表单被我在主页中输入的信息所占据.这就是为什么在上面的示例中,我将POST请求传递给程序视图的原因:

In the second view, I have the same form and I want this form to be pre-occupied with the information I entered in the home page. That is why in the above, I passed my POST request to programs view that is as below:

def programs(request):
    template_name = 'programs.html'
    programs_context = {}
    if request.POST:
        my_form = MyModelForm(request.POST)
        if my_form.is_valid():
            #  do some other stuff
    else:
        my_form = MyModelForm()
    programs_context.update({'my_form': my_form, })
    return render(request, template_name, programs_context)

此策略的缺点(将主视图中的POST请求传递到programs_view)是,网址栏中的url不会更改为'example.com/programs'并保持为'example.com'.我将遇到一些问题,包括程序分页方面的问题.另一种选择是我这样做:

The drawback of this strategy (passing the POST request in the home view to the programs_view) is that the url in url bar does not change to 'example.com/programs' and stays as 'example.com' . I will have some problems including problems in pagination of the programs. The alternative is that I do this:

def home(request):
    template_name = 'home.html'
    home_context = {}
    if request.POST:
        my_form = MyModelForm(request.POST)
        if my_form.is_valid():
            #  do some stuff
            querystring = request.POST
            return redirect(reverse('programs') + '?' + parse_qs(querystring, keep_blank_values=True))
    else:
        my_form = MyModelForm()
    home_context.update({'my_form': my_form, })
    return render(request, template_name, home_context)

首先,执行此操作时出现错误:"QueryDict"对象没有属性"decode"

First Of all, I get an error when I do this: 'QueryDict' object has no attribute 'decode'

第二,我不知道要在程序视图中做什么以利用要发送到程序视图的get分支的查询.

second, I do not know what to do in the programs view to make use of the query I am sending to the get branch of the programs view.

第三,如果它是来自重定向而不是来自独立的直接get请求,那么我需要在程序视图的get分支中的程序视图的post分支中做过的事情.如何在程序获取请求中区分这一点?

Third, I need to the stuff I used to do in the post branch of the programs view in get branch of the programs view if it is from a redirect not from an independent direct get request. How can I distinguish this in programs get request?

总体上,我们高度赞赏任何替代解决方案和帮助.

Overall, any alternative solution and help are highly appreciated.

推荐答案

我有一所房子,里面有一张可以从学生那里获得一些信息的表格,可以为他们建议一些可以应用的程序.主视图如下:

I have a home in which I have a form that I get some info from students to suggest them some programs to apply to. The home view is as below:

def home(request):
    template_name = 'home.html'
    home_context = {}
    if request.POST:
        my_form = MyModelForm(request.POST)
        if my_form.is_valid():
            querystring = urlencode(request.POST)
            return redirect(reverse('programs') + '?' + querystring)

    else:
        my_form = MyModelForm()
    home_context.update({'my_form': my_form, })
    return render(request, template_name, home_context)

,并且在程序视图中

def programs(request):
    template_name = 'programs.html'
    programs_context = {}
    if request.POST:
        my_form = MyModelForm(request.POST)
        if my_form.is_valid():
            #  do some other stuff
    else:
        # if the get is from redirect
            my_form = MyModelForm(request.GET)
            #  do some other stuff
        # else:
            my_form = MyModelForm()
    programs_context.update({'my_form': my_form, })
    return render(request, template_name, programs_context)

我现在想通过检查请求是来自输入example.com/programs还是来自主视图的重定向部分来将程序视图的get方法划分为多个部分.这样做的标准方法是什么?上面写有if和else的标准写法是什么?

I now want to devide the get method of the programs view into to parts by checking whether a request is coming from entering example.com/programs or it coming from the redirect part of the home view. What is the standard method to do this? What is the standard way of writing the commented if and else above?

这篇关于如何使用parse_qs从视图重定向到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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