表单数据处理在哪里? (Django的) [英] Where is form data handled?? (Django)
问题描述
例如,假设具有单一形式的索引模板:
* index.html *
{%从未来添加url%}
< form action ={%url'目录:_results'%}method =post>
名称:< input type =textname =txtField/>
< input type =submitname =submit/>
< / form>
所以现在为了我的观点我有两个版本:
#1版本(1视图):相同的视图显示并处理表单
def index(request):
如果request.method =='POST':#如果表单已经提交...
form = dbForm(request.POST)#一个绑定到POST的表单数据
如果form.is_valid():#所有验证规则通过
field = form.cleaned_data ['txtField']
#doSomething
else:
form = dbForm ()#unbound form
return render(request,'Directories / index.html',{'form':form})
#2版本(2视图):显示表单和一个视图来处理表单数据的一个视图
创建表单(unbound)
def index(request):
form = dbForm()
return render(request, 'Directories / index.html',{'form':form})
#th e视图处理在表单提交期间在索引模板中发送的数据。
def results(request):
如果request.method =='POST':
form = dbForm(request.POST)#绑定到POST数据的表单
如果表单.is_valid():#所有验证规则通过
field = form.cleaned_data ['txtField']
#doSomething
else:
form = dbForm()#unbound form
return render(request,'Directories / index.html',{'form':form})
这里是我的urls.py:
from django.conf.urls import patterns,url
from Directories导入视图
urlpatterns = patterns('',
url(r'^ $',views.index,name ='_ index'),
url(r'^ results $',views.results,name ='_ results'),)
正如你可以看到数据处理在每个版本中执行不同的方式,因此我想知道这些是否是错误的,如果两者都是正确的,那么哪一个被认为是最佳实践。
通常,表单将发布到正在显示的相同视图。
您可以简化视图逻辑,如下所示:
def index(request):
form = dbForm(data = request.POST或None)
如果form.is_valid():#所有验证规则通过
field = form.cleaned_data ['txtField']
#doSomething
return redirect(success_url)
return render(request,'Directories / index.html',{'form':form})
请注意,如果您在成功的表单发布后重定向通常很好,即使您重定向到同一个视图。这样可以防止用户在刷新页面时提示重新发送表单数据。
您应该查看文档在模板中呈现表单:
https:/ /docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
如果你不t渲染字段错误,例如,用户永远不会知道问题是什么。
将发布到不同视图的表单的一个例子是,如果你的基本模板具有出现在每个页面上的搜索表单。当您发布此表单时,您不想回到当前视图,您要转到搜索结果视图。
I have read a lot of tuts and documentation on form creation and handling in Django but I still am confused on certain aspects of the implementation. Specifically, I cannot understand where I should handle the data sent by the form. Is it on the view that is using the form template or is it on another view?
For example, assume an index template with a single form:
*index.html*
{% load url from future %}
<form action="{% url 'Directories:_results'%}" method="post">
Name: <input type="text" name="txtField" />
<input type="submit" name="submit" />
</form>
So now for my view i have two versions:
#1 version (1 view): The same view displays and handles the form
def index(request):
if request.method == 'POST': # If the form has been submitted...
form = dbForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
field = form.cleaned_data['txtField']
#doSomething
else:
form = dbForm() #unbound form
return render(request, 'Directories/index.html', {'form': form})
#2 version (2 views): One view to display the form and one view to handle the form data
#the view that creates the form (unbound)
def index(request):
form = dbForm()
return render(request, 'Directories/index.html', {'form':form})
#the view that handles the data sent during form submission in the index template.
def results(request):
if request.method == 'POST':
form = dbForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
field = form.cleaned_data['txtField']
#doSomething
else:
form = dbForm() #unbound form
return render(request, 'Directories/index.html', {'form': form})
and here is my urls.py:
from django.conf.urls import patterns, url
from Directories import views
urlpatterns = patterns('',
url(r'^$', views.index, name='_index'),
url(r'^results$', views.results, name='_results'),)
As you can see data handling is performed differently in each version and as a result I want to know if any of these is wrong and if both are correct then which one is considered the best practice.
Generally a form will post to the same view it is being displayed on.
You can simplify the view logic like so:
def index(request):
form = dbForm(data=request.POST or None)
if form.is_valid(): # All validation rules pass
field = form.cleaned_data['txtField']
#doSomething
return redirect(success_url)
return render(request, 'Directories/index.html', {'form': form})
Note that it is usually good if you redirect after a successful form post, even if you redirect back to the same view. This prevents the user from being prompted to 'resend form data' if they refresh the page.
You should look at the docs for rendering a form in the template:
https://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
If you don't render the field errors, for example, the user will never know what the problem was.
An example of a form that would post to a different view is if say your base template has a 'search' form which appears on every page. When you post this form you don't want to come back to the current view, you want to go to the 'search results' view.
这篇关于表单数据处理在哪里? (Django的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!