私はシンプルなフォームを持っています。送信をクリックするたびに、空白のページが表示されます。私が間違っているのかわかりません。私はジャンゴが初めてです。ここでいくつかの質問と回答を読みましたが、これを解決できないようです。ご協力いただきありがとうございます
ビュー.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>