2

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

视图.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))

联系人.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>
4

2 回答 2

1

我认为问题是这样的:

return HttpResponse('Invalid header found.')

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

于 2013-05-30T04:15:27.657 回答
1

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

但是,在您的 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"),
于 2013-05-31T23:26:42.093 回答