以下是我認爲應該是bootstrap3模式中的電子郵件提交表單的工作版本的代碼。表單提交後,我被帶到準系統謝謝頁面,但我不認爲電子郵件地址正在發送到數據庫,因爲我在Django admin中看不到。Django表單提交(我認爲)但不發送到數據庫
此外,我認爲這是打破的另一個原因是表單驗證似乎沒有工作。我可以提交一些與電子郵件地址無關的內容,但我仍然被髮送到感謝頁面。
我在我的所有文件中是否存在問題,還是他們被隔離到特定區域?
views.py
from django.shortcuts import render
from temp.models import Email
from forms import EmailForm
def index(request):
if request.POST:
form = EmailForm(request.POST)
if form.is_valid():
email = EmailForm()
email.save()
return render(request, 'temp/thanks.html')
return render(request, 'temp/index.html')
def thanks(request):
return render(request, 'temp/thanks.html')
forms.py
from django import forms
class EmailForm(forms.Form):
email = forms.CharField(max_length=100)
models.py
from django.db import models
class Email(models.Model):
email = models.CharField(max_length=200)
def __unicode__(self): # Python 3: def __str__(self):
return self.email
的index.html
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Sign up for the Beta</h4>
</div>
<div class="modal-body">
<!-- The async form to send and replace the modals content with its response -->
<form class="form-horizontal well" data-async data-target="#myModal" action="thanks/" method="POST">
{% csrf_token %}
<fieldset>
<div>Your email address:
<span>
<input type="text" id="modalInput"></input>
</span>
</div>
</fieldset>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
<!-- End Modal -->
@Rohan感謝更好forms.py
這一點。在你的第二個代碼中,是否改變了models.py或forms.py? – metersk
@Barnaby,那就是表單。 – Rohan
嗯,它仍然沒有提交給數據庫。此外,它似乎仍然接受無效的電子郵件 – metersk