如何在django中区分从HTML表单提交的HTTP请求和从客户端提交的HTTP请求? [英] How to differentiate an HTTP Request submitted from a HTML form and a HTTP Request submitted from a client in django?

查看:105
本文介绍了如何在django中区分从HTML表单提交的HTTP请求和从客户端提交的HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有一个模型,如下所示:

I have a model in django that is as below:

class Student(Model):
    nationality = CharField(max_length=200)

我的表格如下:

class StudentForm(ModelForm):

    class Meta:
        model = Student
        fields = ('nationality', )

我的模板如下:

<form method="GET" novalidate id="my_form">
      {{ student_form.as_p }}
</form>
<button type="submit" form="my_form" name="my_form">Submit</button>

我的看法如下:

def home(request):
    if request.POST:
        return HttpResponse('This should never happen')
    else:
        if request.GET.get('nationality'):
            student_form = StudentForm(request.GET)
            if student_form.is_valid():
                return HttpResponse('get from form submission')
        else:
            student_form = StudentForm()
            print('get from client request')
            return render(request, my_template, {'student_form': student_form}) 

此方法的问题在于,如果sb提交表单时未填写国籍字段,结果将是从客户请求中获取",这是错误的,因为验证错误应该发生,因为该请求是由于提交的表单不是直接提交的客户得到请求. 我可以做的是向表单中添加一个隐藏字段,如下所示:

The problem with this method is that if sb submits the form without filling the nationality field, the result would be 'get from client request' that is wrong because the validation error should happen because the request is from submitting a form not direct client get request. What I can do is that I add a hidden field to my form as below:

<form method="GET" novalidate id="my_form">
      {{ student_form.as_p }}
      <input type="hidden" id="hidden" name="hidden" value="hidden">
</form>
<button type="submit" form="my_form" name="my_form">Submit</button>

并按如下所示更改我的看法:

and change my view as below:

def home(request):
    if request.POST:
        return HttpResponse('This should never happen')
    else:
        if request.GET.get('hidden'):
            student_form = StudentForm(request.GET)
            if student_form.is_valid():
                return HttpResponse('get from form submission')
        else:
            student_form = StudentForm()
            print('get from client request')
            return render(request, my_template, {'student_form': student_form})

但是,应该有另一种方法可以做到这一点. HTTP中应该有一些内容可以告诉我们该请求是来自客户端的新请求,还是来自表单提交的请求.我正在寻找这个.

However, there should be another method to do this. There should be something in HTTP to tell us the request is fresh get request from client or it is from form submission. I am looking for this.

推荐答案

request.GET是字典.如果字典中没有键'nationality',但其值是空字符串(?nationality=),则request.GET.get('nationality')是虚假的.因此,您应该只检查密钥的存在,那样就可以知道表单已提交:

request.GET is a dictionary. request.GET.get('nationality') is falsy if the dictionary doesn't have the key 'nationality' but also if its value is the empty string (?nationality=). So you should just check the presence of the key, that way you know the form was submitted:

if 'nationality' in request.GET:
    # initialise form with `request.GET`
else:
    # initial unbound form

这篇关于如何在django中区分从HTML表单提交的HTTP请求和从客户端提交的HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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