我正在尝试做一个小测验应用程序,随机地问5 quetions.Before我在模型中使用了forein键。我通过一个django document.Now,我想在同一个page.while上呈现所有的等式和它的4个选项,在文档中,它创建了一个链接,在每个字符上只使用它的id呈现一个字符。现在,如果我在同一页上渲染,它就不会为每一张纸分开广播。参考链接:https://docs.djangoproject.com/en/3.0/intro/tutorial01/我的代码如下所示:
models.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = 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_textviews.py
def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list': latest_question_list}
return render(request, 'index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'detail.html', {'question': question})
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'results.html', {'question': question})
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('app1:results', args=(question.id,)))index.html这里创建了链接
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'app1:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}detail.html在单击quetion之后,它出现在下面的页面中,其中显示了带有ans的符号格式。
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'app1:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
<input type="submit" value="Vote">
</form>发布于 2020-05-14 08:27:59
当您按submit时,将所有输入数据作为参数发送到post,因此,如果您将所有输入放在一种形式中,它将发送全部输入数据。您可以用一个名称来计算数据,您可以调用第一个输入1和第二个输入2等等,在服务器端,您应该检查所有的输入,如果答案确定,您也可以使用js发送您曾经去过的东西。
https://stackoverflow.com/questions/61776984
复制相似问题