html - for循环中的几个标签
问题描述
我是编程新手,我完成了 Django 教程“编写你的第一个 Django 应用程序”。现在我想对这个应用程序进行一些更改,但我真的不知道该怎么做。我想在以下输入for循环中为每个choice.id设置几个带有不同图像的标签:
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls: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>
模型.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
class Choice(models.Model):
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
question = models.ManyToManyField(Question)
def __str__(self):
return self.choice_text
视图.py
def index(request):
latest_question_list = Question.objects.order_by('pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/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, 'polls/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('polls:results', args=(question.id,)))
我真的很感激对此的一些帮助。谢谢!
解决方案
推荐阅读
- angular - 这个 switchmap 的论点从何而来?
- c - 用 C 和 libao 同时播放多个 wav 音频
- apache-spark - 在 Spark 中对 DataFrame 进行排序时,幕后会发生什么?
- c# - 从后端 c# 代码单击 hfref 打开一个弹出窗口
- .net - ODP.NET - 连接池未填充
- ios - 使用谓词的字典过滤器数组 - swift3
- excel - 如何使 Application.key Shortcuts WorkBook 具体化?
- python - (Python) 用未知数求解方程,用方程求出该数
- amazon-web-services - 在 S3 中隐藏网页内容
- java - 指定目录时出现 Java.io.FileNotFoundException