创建投票应用程序表单Djang

2024-05-10 05:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

试图为我的投票创建一个表单应用程序。但是我不能包括选择,以及它。我只能添加投票现在通过管理员。 (这是django民意调查应用程序的官方教程,我正试图通过添加一个表单来进一步扩展它)

在模型.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

在表单.py在

^{pr2}$

查看以创建投票

class CreatePoll(CreateView):
    redirect_field_name = 'polls/index.html'
    template_name = 'polls/poll_form.html'
    form_class = QuestionForm
    model = Question

这是我的表单视图,但是输入的选项没有得到救了。锯在管理视图中也是如此 My Form View Appears Like this But the choice entered doesnt get saved.Saw in the admin view too it was not saved

enter image description here

投票_表单.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 %}

Tags: textpyselfform应用程序表单returnmodels