【问题标题】:Custom Django forms within template and hidden inputs模板和隐藏输入中的自定义 Django 表单
【发布时间】:2017-11-18 10:59:53
【问题描述】:

非常非常快速的问题。我正在我的 html 模板中呈现我自己的自定义表单...当用户提交该表单时,我将如何提交隐藏的输入?

模板.html

<form class="contact-form" name="contact-form" method="post" action=".">
  {% csrf_token %}
  <div class="row">
    <div class="col-sm-12">
      <div class="form-group">
        <label for="four">Your Text</label>
        <textarea class="form-control" type="text" name="{{ comment_form.content.name }}" {% if comment_form.content.value %}value="{{ comment_form.content.value }}"{% endif %} placeholder="" required="required" id="four"></textarea>
      </div>
    </div>
  </div>
  <div class="form-group text-center">
    <button type="submit" class="btn btn-primary pull-right">Submit Comment</button>
  </div>
</form>

form.py

from django import forms
from .models import Comment

class CommentForm(forms.ModelForm):
    content_type = forms.CharField(widget=forms.HiddenInput)
    object_id = forms.IntegerField(widget=forms.HiddenInput)
    #parent_id = forms.IntegerField(widget=forms.HiddenInput, required=False)
    content = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Comment
        fields = ('content',)

【问题讨论】:

  • 你的views.py代码在哪里?添加你的views.py代码

标签: django django-forms django-templates


【解决方案1】:

您可以将此部分添加到您的 forms.py 文件中(在 CommentForm 类中):

    hidden_field=forms.CharField(widget=forms.HiddenInput())

我希望它会起作用。让我知道

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 2018-04-14
    • 2018-03-24
    • 2022-01-11
    • 2014-07-12
    • 2017-05-02
    • 1970-01-01
    • 2013-03-28
    相关资源
    最近更新 更多