Django 1.5简单形式

时间:2013-05-30 02:36:57

标签: django django-forms

我有一个简单的表格。每当我点击提交,我都会得到一个空白页面。不确定我做错了什么。我是django的新手。我在这里已经阅读了几个问题和答案,但我似乎无法解决这个问题。谢谢你的帮助

views.py

from django.views.decorators.csrf import csrf_exempt, requires_csrf_token, csrf_protect
from django import http
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings

from contact import ContactForm

from django.template import RequestContext, Context

from django import forms
from django.core.mail import send_mail, BadHeaderError
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf

@csrf_protect
def contactview(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST)
        #return HttpResponse('Invalid header found.') I edited and removed this
        if form.is_valid():
            subject = form.cleaned_data['subject']
            name = form.cleaned_data['name']
            sender = form.cleaned_data['sender']
            message = "The following feedback was submitted from %s  \n\n" % (sender)
            message += form.cleaned_data['message']
            recipients = ['messages@example.com']
            cc_myself = form.cleaned_data['cc_myself']
            if cc_myself:
                recipients.append(sender)
            try:
                send_mail(subject, message, sender, recipients, fail_silently=False)
                return HttpResponseRedirect('/thankyou/')
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
    else:
        form = ContactForm()

    return render_to_response('contact.html', {'form': form}, context_instance=RequestContext(request))

contact.py

from django import forms 

# A simple contact form with five fields.
class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    name = forms.CharField(max_length=100)
    message = forms.CharField(widget=forms.Textarea())
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

在我的模板中

<form action="" method="post">{% csrf_token %}   
    <tr><th><label for="id_sender">Your email:</label></th>
        <td><input class="text" type="text" name="sender" id="id_sender" /></td></tr>
    <tr><th><label for="id_sender">Name:</label></th>
        <td><input class="text" type="text" name="name" id="id_name" /></td></tr>       
    <tr><th><label for="id_subject">Subject:</label></th>
        <td><input class="text" id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>
    <tr><th><label for="id_message">Message:</label></th>
        <td><textarea class="styletextarea" name="message" id="id_message" rows="10" cols="35" /></textarea></td></tr>
    <tr><th><label for="id_cc_myself">Cc myself:</label></th>
        <td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>
    <tr><td></td><td><input class="button" type="submit" value="Send" /></td></tr>
</form>
</table>
</div>

2 个答案:

答案 0 :(得分:1)

我认为问题是:

return HttpResponse('Invalid header found.')

在您实例化表单后。删除该行,执行应该继续正常。

答案 1 :(得分:1)

在urls.py中,视图函数被视为“基于类的视图”。为了在基于类的视图中使用post / get,您必须定义一个方法,告诉视图类如何处理发布请求。

然而,在views.py中,视图函数不是基于类的视图,它只是一个视图函数。所以我更改了urls.py中的URL以指示它只是一个函数,而不是一个类。

有关详细信息,请参阅此https://docs.djangoproject.com/en/dev/topics/class-based-views/

url(r'^contact/$', contactview, name="contactview"),
url(r'^thankyou/$', thankyou, name="thankyou"),