英文:
Django forms's logic for a form in a base template
问题
I'm trying to figure out how to deal with this situation. I have three views:
def view0(request):
...
return render(request, 'home.html')
def view1(request):
...
return render(request, 'about.html')
Both view templates extend a template called 'base.html', in which there is a contact form.
What I'm wondering is: do I need to write Python code in both views to handle all the logic inherent in the form's operation? Or can I create a view whose only job is to handle the logic of this specific form?
I tried to create a separate view, assigning it a URL to which the 'action' attribute of the form refers:
def base_view(request):
if request.method == 'POST':
contact_form = Form(request.POST)
#logic here
else:
contact_form = Form()
context = {'contact_form': contact_form}
return render(request, 'base.html', context)
def view0(request):
...
return render(request, 'home.html')
def view1(request):
...
return render(request, 'about.html')
'base.html' has the following code for the form:
.....
<form action="{% url 'base_view_url' %}" method="post">
{% csrf_token %}
{{contact_form}}
<input type="submit" value="Invia">
</form>
.....
With this code, the contact form does not work.
英文:
I'm trying to figure out how to deal with this situation. I have three views:
def view0(request):
...
return render(request, 'home.html')
def view1(request):
...
return render(request, 'about.html')
Both view templates extends a template called 'base.html', in which there is a contact form.
What I'm wondering is: do I need to write python code in both views to handle all the logic inherent in the form's operation? Or can I create a view whose only job is to handle the logic of this specific form?
I tried to create a separate view, assigning it a url to which the 'action' attribute of the form refers:
def base_view(request):
if request.method == 'POST'
contact_form = Form(request.POST)
#logic here
else:
contact_form = Form()
context = {'contact_form': contact_form}
return render (request, 'base.html', context)
def view0(request):
...
return render(request, 'home.html')
def view1(request):
...
return render(request, 'about.html')
'base.html' has the following code for the form:
.....
<form action="{% url 'base_view_url' %}" method="post">
{% csrf_token %}
{{contact_form}}
<input type="submit" value="Invia">
</form>
.....
With this code the contact form does not work.
答案1
得分: 1
你需要回到基础部分来理解表单的工作原理。
- 每个表单都有一个操作URL,在表单提交时将调用该URL。
- 一旦用户提交表单,浏览器将对表单数据进行编码并将其发送到操作URL。
- 一旦Django接收到操作URL,它将与URL模式匹配以决定调用哪个视图。
- 视图将处理数据并向用户返回响应。
所以,回答你的问题。
你只需要一个视图来处理表单(假设两个视图中的表单相同),并且你可以将操作URL设置为始终调用'base_view_url'。
此外,页面可以具有多个表单,每个表单具有不同的操作URL。
英文:
You need to go back a bit to the basics to understand how forms work.
- Each form has an action URL, which will be called once the form is submitted.
- Once the user submits the form, the browser will encode the form data and send it to action URL.
- Once the action URL is received by Django, it will match the URL against the URL patterns to decide which view to call.
- The view will process the data and return a response to the user.
So to answer your question.
You need only one view to handle the form (given that the form is the same in both views) and you can set the action URL to always call 'base_view_url'.
Also, the page can have multiple forms, each with different action URL.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论