我试图让一个非常简单的电子邮件表单工作。表格只有一个字段(电子邮件)。
表格.py
from django import forms
class EmailForm(forms.Form):
email = forms.EmailField()
视图.py
from django.shortcuts import render_to_response
from django.core.mail import EmailMultiAlternatives
from django.template import RequestContext
from launchpage.models import EmailForm
def index(request):
if request.method == 'POST':
form = EmailForm(request.POST)
if form.is_valid():
clean_data = form.clean_data
# send an email
thank_you = "Thanks for signing up"
return render_to_response('launch.html', {'thank_you': thank_you})
else:
form = EmailForm()
return render_to_response('launch.html', {'form': form,}, context_instance=RequestContext(request))
launch.html(相关部分)
<div class="row-fluid">
<div class="span12">
{% if thank_you %}
<div class="alert alter-info">
{{ thank_you }}
</div>
{% else %}
<form action="" method="post" class="form-horizontal">{% csrf_token %}
Or stay updated by email (no spam, pinky swear)
{{ form.email }}
<button type="submit" class="btn">Submit</button>
</form>
{% endif %}
</div>
</div>
settings.py 中的中间件
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
当前结果:页面显示好像没有涉及模板。我看到了表单和提交按钮,但没有电子邮件字段。也没有csrf隐藏字段。我一定是错过了一些愚蠢的东西......