自定义Django在模板和隐藏输入中形成

时间:2017-06-15 20:08:05

标签: django django-forms django-templates

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

template.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',)

1 个答案:

答案 0 :(得分:0)

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

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

我希望它能奏效。我知道