I was following the Definitive Guide To Django (v2)
Here's the traceback if you want to take a look http://dpaste.com/344698/
It gives me this error:
TemplateSyntaxError at /contact/
Could not parse the remainder: '-mail' from 'e-mail'
Template error
In template /home/jwxie/django-dev/mysite/contact/template/contact_form.html, error at line 5
and line 5 is {% block content %}
The code I had in the views.py
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.mail import send_mail
def contact(request):
display_error = []
if request.method == 'POST':
if not request.POST.get('subject',''):
display_error.append('Enter a subject')
if not request.POST.get('message',''):
display_error.append('Enter a message')
if not request.POST.get('e-mail') and '@' not in request.POST['e-mail']:
display_error.append('Enter a valid e-mail address')
if not display_error:
send_mail(
request.POST['subject'],
request.POST['message'],
request.POST.get('e-mail','noreply@example.com'),
['siteowner@example.com'],
)
return HttpResponseRedirect('/contact/thanks/')
return render_to_response('contact_form.html',{
'subject': request.POST.get('subject',''),
'message': request.POST.get('message',''),
'e-mail': request.POST.get('e-mail',''),
'display_error': display_error,
})
def contact_thanks(request):
return HttpResponse('Thanks')
This is the code in contact_form.html
{% extends "base.html" %}
{% block title %} Contact {% endblock %}
{% block content %}
{% block content-h1 %}<p>Feel free to contact us!</p> {% endblock %}
<form action="/contact/" method="post">
<p>Subject: <input type="text" name="subject" value="{{ subject }}"></p>
<p>Your e-mail (optional): <input type="text" name="e-mail" value="{{ e-mail }}"> </p>
<p>Message: <textarea name="message" rows="10" cols="50">{{ message }}</textarea></p>
<input type="submit" value="Submit">
</form>
{% if display_error %}
<ul>
{% for error in display_error %}
<li>{{error}}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
I had contact as app, the structure looks like this: mysite/contact and it has its own template mysite/contact/template I am sure the setting.py is correct...开发者_运维知识库
what do you guys think? any help is appreciated. I am just playing around with the sample code.
Why do you write "e-mail"? Don't the template variables have to be a Python variable names? "e_mail"?
http://docs.djangoproject.com/en/dev/topics/templates/#variables
It says
Variable names consist of any combination of alphanumeric characters and the underscore ("_").
精彩评论