简体   繁体   English

表单如何在Django中运作

[英]How forms work in django

I am sure this is a silly question but I am trying to understand how forms work in django and I am confused about the action attribute in the template and the httpresponseredirect in views.py 我敢肯定这是一个愚蠢的问题,但是我试图理解表单如何在Django中工作,并且我对模板中的action属性和views.py中的httpresponseredirect感到困惑

I have the following template: 我有以下模板:

<form action="/play_outcomes/output/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

And the following view: 和以下视图:

def getinput(request):
     if request.method == 'POST':
         form = get_data(request.POST)

         if form.is_valid():
             down = form.cleaned_data['get_down']
             ytg = form.cleaned_data['get_ytg']
             yfog = form.cleaned_data['get_yfog']
             return HttpResponseRedirect('/thanks/')
     else:
         form = get_data()


     return render(request, 'play_outcomes/getinput.html', {'form': form})

And finally in forms.py 最后是forms.py

class get_data(forms.Form):
    get_down = forms.IntegerField(label = "Enter a down")
    get_ytg = forms.IntegerField(label = "Enter yards to go")
    get_yfog = forms.IntegerField(label = "Enter yards from own goal")

When I go to play_outcomes/getinput my form comes up. 当我去play_outcomes/getinput我的形式出现。 When I press 'submit' it directs shows the play_outcomes/output page, presumably because that is what is specified by the <form action variable. 当我按“提交”时,它会直接显示play_outcomes/output页面,大概是因为这是<form action变量指定的。

Anyway, my question is what is the purpose of the return HttpResponseRedirect('/thanks/') after it has checked form.is_valid() . 无论如何,我的问题是在检查form.is_valid()之后返回HttpResponseRedirect('/thanks/')的目的是什么。 This is TRUE so it must execute this code. 这是TRUE因此必须执行此代码。 But this return seems redundant. 但是这种回报似乎是多余的。

Please can someone enlighten me on what the return in the views.py is doing 请有人能启发我views.pyreturn

Try replacing the form action with action="" . 尝试将表单操作替换为action="" Everything should continue working normally, since your view handles both GET requests (to show an empty form) and POST requests (to process when a form is submitted). 一切都应继续正常工作,因为您的视图可以处理GET请求(显示一个空表单)又可以处理POST请求(处理提交表单时的请求)。 When you press "Submit", your browser is sending the form data back to the exact same place it got the original web page from. 当您按“提交”时,浏览器会将表单数据发送回与原始网页完全相同的位置。 Same URL, same view ( getinput() ), same template file ( play_outcomes/getinput.html ). 相同的URL,相同的视图( getinput() ),相同的模板文件( play_outcomes/getinput.html )。

Normally, right before that HttpResponseRedirect() part, you'd include some logic to save the content of that form, email a user, do some calculation, etc. At that point, since the user is all done with that page, you typically redirect them to somewhere else on your site. 通常,就在HttpResponseRedirect()部分之前,您将包括一些逻辑来保存该表单的内容,向用户发送电子邮件,进行一些计算等。此时,由于用户已经完成了该页面,因此通常将它们重定向到您网站上的其他位置。

Edit: empty action 编辑:空动作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM