如何使用基于类的视图在Django中的一个模板上呈现两个不同的表单?
在django基本民意测验应用程序中。我已经添加了添加投票的功能,我希望“添加选择”表单位于模板上“投票详细信息”表单的下方,我该怎么做 我希望“添加选项”呈现在“详细信息”模板的投票下面 添加选项视图:如何使用基于类的视图在Django中的一个模板上呈现两个不同的表单?,django,Django,在django基本民意测验应用程序中。我已经添加了添加投票的功能,我希望“添加选择”表单位于模板上“投票详细信息”表单的下方,我该怎么做 我希望“添加选项”呈现在“详细信息”模板的投票下面 添加选项视图: def choice_view(request,question_id): if request.user.is_staff and request.user.has_perm('poll.add_choice'): question=Question.objects.
def choice_view(request,question_id):
if request.user.is_staff and request.user.has_perm('poll.add_choice'):
question=Question.objects.get(pk=question_id)
if request.method=='POST':
cform=choice_form(request.POST)
if cform.is_valid():
add_poll=cform.save(commit=False)
add_poll.question=question
add_poll.vote=0
add_poll.save()
cform.save()
return HttpResponseRedirect(reverse('polls:add_choice',args=(question_id,)))
else:
cform=choice_form()
return render(request,'polls/add_choice.html',{'cform':cform,'question_id':question_id},context_instance=RequestContext(request),)
else:
return HttpResponse('Permission Denied')
添加选项模板:
{% load crispy_forms_tags %}
{% include 'polls/base2.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Choices</title>
</head>
<body>
<h1>Add Choices</h1>
<form method="post">
{% csrf_token %}
{{ cform.choice_text | as_crispy_field }}
<button class="btn btn-primary btn-default" type="submit">Add Next</button>
<a href="{% url 'polls:detail' question_id %}" class="btn btn-warning">Done</a>
</form>
</body>
</html>
{% load staticfiles %}
{% include 'polls/base2.html' %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
{% 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 %}
{% if choice.is_voted_by%}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }} " checked="checked" />
{% else%}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }} " />
{% endif%}
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br/>
{% endfor %}
<br><button class="btn btn-lg btn-primary btn-block" type="submit" style="width: 100px">Vote</button>
</form>
<br><a href="{% url 'polls:comment' question.id %}" class="btn btn-primary btn-default">Leave a comment</a>
{% if user.is_staff %}
<a href="{% url 'polls:question_delete' question.id %} " class="btn btn-danger">Delete question</a>
{% endif %}
<hr>
<h3>Comments</h3>
{% for comment in question.comments.all %}
<br>{{ comment.body }}
{% if user.is_staff %}
<a href="{% url 'polls:comment_delete' question.id comment.id %} ">Delete comment</a>
{% endif %}
<br>--{{ comment.email }}<br>
{% endfor %}
</body>
</html>
投票详情视图:
class DetailView(generic.DetailView):
model=Question
template_name = 'polls/detail.html'
投票详细信息模板:
{% load crispy_forms_tags %}
{% include 'polls/base2.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Choices</title>
</head>
<body>
<h1>Add Choices</h1>
<form method="post">
{% csrf_token %}
{{ cform.choice_text | as_crispy_field }}
<button class="btn btn-primary btn-default" type="submit">Add Next</button>
<a href="{% url 'polls:detail' question_id %}" class="btn btn-warning">Done</a>
</form>
</body>
</html>
{% load staticfiles %}
{% include 'polls/base2.html' %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
{% 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 %}
{% if choice.is_voted_by%}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }} " checked="checked" />
{% else%}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }} " />
{% endif%}
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br/>
{% endfor %}
<br><button class="btn btn-lg btn-primary btn-block" type="submit" style="width: 100px">Vote</button>
</form>
<br><a href="{% url 'polls:comment' question.id %}" class="btn btn-primary btn-default">Leave a comment</a>
{% if user.is_staff %}
<a href="{% url 'polls:question_delete' question.id %} " class="btn btn-danger">Delete question</a>
{% endif %}
<hr>
<h3>Comments</h3>
{% for comment in question.comments.all %}
<br>{{ comment.body }}
{% if user.is_staff %}
<a href="{% url 'polls:comment_delete' question.id comment.id %} ">Delete comment</a>
{% endif %}
<br>--{{ comment.email }}<br>
{% endfor %}
</body>
</html>
{%load staticfiles%}
{%include'polls/base2.html%}
{%load crispy_forms_tags%}
标题
{{question.question_text}
{%if错误消息%}
{{error\u message}}
{%endif%}
{%csrf_令牌%}
{问题中的选项为%choice\u set.all%}
{%if choice.u是否由%}
{%else%}
{%endif%}
{{choice.choice_text}}
{%endfor%}
投票
{%if user.is_staff%}
{%endif%}
评论
{%用于有问题的评论。评论。所有%}
{{comment.body}}
{%if user.is_staff%}
{%endif%}
--{{comment.email}}
{%endfor%}
如果没有视图、表单和模板,将无法为您提供帮助。我已添加了必需的内容:)请遵循以下步骤