尝试为我的民意调查应用创建一个表单。但我无法为其添加选项。我现在只能通过管理员添加民意调查。 (它是官方的django民意调查应用程序教程,我试图通过添加表单进一步扩展它)
Models.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
publish_date = models.DateTimeField('date published',null=True,blank=True)
def __str__(self):
return self.question_text
def get_absolute_url(self):
return reverse('polls:index')
class Choice(models.Model):
question_text = models.ForeignKey(Question,on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
forms.py
class QuestionForm(forms.ModelForm):
question_text = forms.CharField(required=True)
publish_date = forms.CharField(required=False)
choice_text = forms.CharField(required=True)
class Meta():
model = Question
fields = ('question_text','choice_text')
查看创建投票
class CreatePoll(CreateView):
redirect_field_name = 'polls/index.html'
template_name = 'polls/poll_form.html'
form_class = QuestionForm
model = Question
这是我的表单视图但是输入的选项并没有得到保存。也在管理视图中
polls_form.html
{% extends 'polls/base.html' %}
{% block content %}
<h1>New Post</h1>
<form class="post-form" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="save btn btn-primary">Save</button>
</form>
<script>var editor = new MediumEditor('.editable');</script>
{% endblock %}