【发布时间】:2015-11-09 10:39:48
【问题描述】:
我尝试使用 django & bootstrap 建立一个网站,在其中一个页面中我有两种形式,一种是在帖子中添加评论,另一种是与我联系,联系表在引导模式上。如何使用基于功能的视图将联系表单放入模态?
views.py
def GameView(request, slug):
game = GameUpload.objects.get(slug=slug)
comment_form = CommentForm()
contact_form = ContactForm()
if request.method == "POST":
if request.POST['form-type'] == u'comment-form':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
instance = form.save(commit=False)
instance.upload = game
instance.save()
else:
contact_form = ContactForm(request.POST)
#Send mail to me
context = {"game": game,
"comment_form": comment_form,
"contact_form": contact_form,
"comments": game.comment_set.all().order_by("-date")}
return render(request, 'game.html', context)
模板
<div class="jumbotron">
<div class="container">
<div class="col-sm-8 col-sm-offset-2">
<h3><i class="fa fa-comment"></i> Add Comment:</h3>
<form method="POST">
{% csrf_token %}
{{ comment_form|crispy }}
<input type="hidden" name="form-type" value="comment-form" />
<input type="submit" value="Submit" class="btn btn-primary" />
</form>
</div>
</div>
</div>
模态:
<div id="contact" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Contact Me</h4>
</div>
<div class="modal-body">
<form method="POST" action="/">
{% csrf_token %}
{{ contact_form|crispy }}
<input type="hidden" name="form-type" value="contact-form" />
</form>
</div>
<div class="modal-footer">
<input type="submit" value="Send" class="btn btn-success" />
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
【问题讨论】:
标签: python django django-forms django-views