0
我試圖建立一個網站使用django & bootstrap,並在其中一個頁面我有兩種形式,一種是添加評論的職位,另一種是與我聯繫,聯繫表格是在自舉模式上。我怎樣才能把聯繫表格放在模態中,用基於功能的視圖?如何將django窗體與另一個窗體在同一頁(模式外部)上的引導模態?
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>
好的,我沒有注意到,但仍然,按下模式關閉按鈕後,你需要重新打開它,看看你沒有填滿所有的領域 – Ofersto
這是因爲頁面重新加載。如果你想驗證表單,你將需要使用Ajax。 – Othman
此外,請檢查這個http://stackoverflow.com/questions/11276100/how-do-i-insert-a-django-form-in-twitter-bootstrap-modal-window – Othman