0
我試圖得到一個非常簡單的電子郵件的形式工作。表格只有一個字段(電子郵件)。Django的煩惱與形式和CSRF令牌
forms.py
from django import forms
class EmailForm(forms.Form):
email = forms.EmailField()
views.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隱藏字段。我一定是失去了一些東西愚蠢......
我不知道爲什麼你有一個downvote,那不是我。我解決了你提到的問題,並且一開始它就不起作用。然後我嘗試重新啓動Apache,它的工作!謝謝 – n0pe 2012-04-18 05:44:07