一次处理django视图中的所有模板问题

时间:2017-01-13 01:32:15

标签: python django

大家好!我是一个新手。使用django文档示例,我如何处理#the views.py

中的所有问题

模板:



<html>

<body>
  {% for question in latest_question_list %}
  <h1>{{ question.question_text }}</h1>
  {% if error_message %}
  <p><strong>{{ error_message }}</strong>
  </p>
  {% endif %}
  <form id="qform" action="{% url 'polls:vote' question.id %}" method="post">{% csrf_token %} {% for choice in question.choice_set.all %}
    <input type="radio" name="{{ question.id }}" id=”choice{{ forloop.counter }} " value="{{ choice.id }}” />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
    <br/>{% endfor %}
  </form>
  {% endfor %}
  <input form="qform" type="submit" value="Vote" />
</body>

</html>
&#13;
&#13;
&#13;

views.py(这不起作用!)

def vote(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    try: 
        selected_choice = question.choice_set.get(pk=request.POST[question.id])
    except (KeyError, Choice.DoesNotExist):  
        return render(request, 'polls/detail.html', { 'question':
         question, 'error_message': "You didn't select a choice.", }) 
    else: selected_choice.votes += 1 selected_choice.save()  
        return render(request,'polls/results.html', {'question':question,
         'selected_choice': selected_choice, 'error_message': error_message})

models.py

class Question(models.Model): 
    question_text = models.CharField(max_length=200) 
    pub_date = models.DateTimeField(‘date published’)


class Choice(models.Model): 
    question = models.ForeignKey(Question) 
    choice_text = models.CharField(max_length=200) 
    votes = models.IntegerField(default=0)

forms.py

class QuestionForm(forms.ModelForm):
    class Meta:
        model = Question
        fields = [
        'question_text',
        'choices',
        ]

    choices = forms.ModelMultipleChoiceField(queryset=Question.choice_set, widget=forms.RadioSelect)

请告诉我我错过了什么;我非常需要帮助。谢谢大家!

2 个答案:

答案 0 :(得分:1)

您为每个问题使用了单独的表单,但浏览器只接受一个表单来发送给服务器,您必须使用django Formsets来解决此类问题。

<强>更新
此通知表示您在每个表单上只放置了一个输入很重要,您可以通过一些更改并且不使用 Django Formsets 来执行此操作:

我认为你的models.py类似于:

from django.db import models


class Question(models.Model):
    body = models.CharField(max_length=200)
    answer = models.CharField(max_length=200)


class Quiz(models.Model):
    questions = models.ManyToManyField(Question)

您可以在forms.py中创建一个表单类来处理它:

from django import forms

from models import Quiz, Question

class QuizForm(forms.ModelForm):
    class Meta:
        model = Quiz
        fields = '__all__'

    questions = forms.ModelMultipleChoiceField(queryset=Question.objects.all(), widget=forms.CheckboxSelectMultiple)

您的views.py文件:

from django.shortcuts import render

from forms import QuizForm

def show(request):
    return render(request, 'qtemplate.html', {'form': QuizForm()})

并在模板上:

<form method="post">
    {{ form }}
</form>

答案 1 :(得分:0)

不确定这是否是一个好的解决方案,但您可以尝试将“提交”输入标记移动到您的表单中:)

<html>

<body>
  {% for question in latest_question_list %}
  <h1>{{ question.question_text }}</h1>
  {% if error_message %}
  <p><strong>{{ error_message }}</strong>
  </p>
  {% endif %}
  <form id="qform" action="{% url 'polls:vote' question.id %}" method="post">{% csrf_token %} {% for choice in question.choice_set.all %}
    <input type="radio" name="{{ question.id }}" id=”choice{{ forloop.counter }} " value="{{ choice.id }}” />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
    <br/>{% endfor %}
  </form>
  <input form="qform" type="submit" value="Vote" />
  {% endfor %}
  
</body>

</html>

或者你可以尝试使用Django-restframework +一些javascript框架(例如react)来实现这个功能