我之前发过一个类似的问题,但答案并没有让我找到解决方案。我相信解决方案现在位于我的模板中,许多答案似乎都没有解决。
无论如何,我认为views.py,forms.py和models.py是正确的,我的问题似乎是调用Bootstrap3模式中的表单,我不太确定如何做到这一点。
这个问题是,填写表单时,验证不会发生,电子邮件也不会发送到数据库。但是,在表单提交上,您将被发送到thanyou页面,所以至少我知道它正常工作...
任何方向都会有所帮助。
的index.html
<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">
<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="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
views.py
from django.shortcuts import render
from temp.models import Email
from temp.forms import EmailForm
def index(request):
if request.method == 'POST':
# POST, get the data from the form
form = EmailForm(request.POST)
if form.is_valid():
# if is valid, just save, and return a new page with thanks
form.save()
return render(request, 'temp/thanks.html')
else:
# Form has errors, re-render with data and error messages
return render(request, 'temp/index.html', {'form': form,})
else:
# GET, render new empty form
form = EmailForm()
return render(request, 'temp/index.html', {'form': form,})
def thanks(request):
return render(request, 'temp/thanks.html')
forms.py
from django.forms import ModelForm
from temp.models import Email
class EmailForm(ModelForm):
class Meta:
model = Email
models.py
from django.db import models
class Email(models.Model):
email = models.EmailField(max_length=200)
def __unicode__(self): # Python 3: def __str__(self):
return self.email
答案 0 :(得分:0)
thanks/
,然后直接调用
def thanks(request):
return render(request, 'temp/thanks.html')
不在数据库中添加任何内容
尝试替换模板:
<form class="form-horizontal well" data-async data-target="#myModal" action="index/" method="POST"> <!-- Or whatever the url is for index -->
如果不起作用:
尝试打印表单本身及其字段的值,以查看它是否包含电子邮件地址。
另外,尝试将文本输入更改为电子邮件输入,当您调用EmailForm演员时,Django表单可能需要将其作为电子邮件输入。