0

我试图让一个非常简单的电子邮件表单工作。表格只有一个字段(电子邮件)。

表格.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) &nbsp;&nbsp;
            {{ 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隐藏字段。我一定是错过了一些愚蠢的东西......

4

1 回答 1

1

假设这个:

from launchpage.models import EmailForm

不应该from launchpage.forms import EmailForm

您遇到的另一个问题是{'form': form,}需要{'form': form}

最后,{{ form.email }}应该是简单的{{ form }}

最后提示 - 考虑使用自动发送适当请求上下文的render快捷方式。

于 2012-04-18T04:07:31.793 回答