Django表单的基础模板中的表单逻辑

huangapple go评论53阅读模式
英文:

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, &#39;home.html&#39;)

def view1(request):
    ...
    return render(request, &#39;about.html&#39;)

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 == &#39;POST&#39;
        contact_form = Form(request.POST)
        #logic here
    else:
        contact_form = Form()
    context = {&#39;contact_form&#39;: contact_form}
    return render (request, &#39;base.html&#39;, context)

def view0(request):
    ...
    return render(request, &#39;home.html&#39;)

def view1(request):
    ...
    return render(request, &#39;about.html&#39;)

'base.html' has the following code for the form:

.....

&lt;form action=&quot;{% url &#39;base_view_url&#39; %}&quot; method=&quot;post&quot;&gt;
   {% csrf_token %}
   {{contact_form}}
   &lt;input type=&quot;submit&quot; value=&quot;Invia&quot;&gt;
&lt;/form&gt;

.....

With this code the contact form does not work.

答案1

得分: 1

你需要回到基础部分来理解表单的工作原理。

  1. 每个表单都有一个操作URL,在表单提交时将调用该URL。
  2. 一旦用户提交表单,浏览器将对表单数据进行编码并将其发送到操作URL。
  3. 一旦Django接收到操作URL,它将与URL模式匹配以决定调用哪个视图。
  4. 视图将处理数据并向用户返回响应。

所以,回答你的问题。
你只需要一个视图来处理表单(假设两个视图中的表单相同),并且你可以将操作URL设置为始终调用'base_view_url'。
此外,页面可以具有多个表单,每个表单具有不同的操作URL。

英文:

You need to go back a bit to the basics to understand how forms work.

  1. Each form has an action URL, which will be called once the form is submitted.
  2. Once the user submits the form, the browser will encode the form data and send it to action URL.
  3. Once the action URL is received by Django, it will match the URL against the URL patterns to decide which view to call.
  4. 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.

huangapple
  • 本文由 发表于 2023年4月17日 14:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定