Python新手使用的Django架站的16堂课 — 第八章(四)
page 236
python manage.py runserver 192.168.99.218:8080
8.2.2 结合窗体和数据库
创建 mysite / form.py
#_*_ encoding: utf-8 *_*
from django import forms
from mysite import models
class ContactForm(forms.Form):
CITY = [
['TP', 'Taipei'],
['TY', 'Taoyuang'],
['TC', 'Taichung'],
['TN', 'Tainan'],
['KS', 'Kaohsiung'],
['NA', 'Others'],
]
user_name = forms.CharField(label='您的姓名', max_length=50, initial='李大仁')
user_city = forms.ChoiceField(label='居住城市', choices=CITY)
user_school = forms.BooleanField(label='是否在学', required=False)
user_email = forms.EmailField(label='电子邮件')
user_message = forms.CharField(label='您的意见', widget=forms.Textarea)
###### 修改urls.py
from django.conf.urls import include, url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^$', views.index),
url(r'^(\d+)/(\w+)/$', views.index),
url(r'^list/$',views.listing),
url(r'^post/$',views.posting),
url(r'^contact/$', views.contact),
url(r'^admin/', include(admin.site.urls)),
]
修改views.py
#encoding:utf-8
from django.shortcuts import render
# _*_ encoding:utf-8 _*_
from django.core.mail import EmailMessage
from django.template import RequestContext
from django.template import Context, Template
from django.template.loader import get_template
from django.http import HttpResponse, HttpResponseRedirect
from mysite import models, forms
def contact(request):
if request.method == 'POST':
form = forms.ContactForm(request.POST)
if form.is_valid():
message = "感谢您的来信."
user_name = form.cleaned_data['user_name']
user_city = form.cleaned_data['user_city']
user_school = form.cleaned_data['user_school']
user_email = form.cleaned_data['user_email']
user_message = form.cleaned_data['user_message']
else:
message = "请检查您输入的信息是否正确!"
else:
form = forms.ContactForm()
template = get_template('contact.html')
request_context = RequestContext(request)
request_context.push(locals())
html = template.render(request_context)
return HttpResponse(html)
修改 contact.html
<!-- contact.html (ch08www project) -->
{% extends "base.html" %}
{% block title %}联络管理员{% endblock %}
{% block content %}
<div class='container'>
{% if message %}
<div class='alert alert-warning'>{{ message }}</div>
{% endif %}
<div class='panel panel-primary'>
<div class='panel-heading'>
<form name='my form' action='.' method='POST'>
{% csrf_token %}
<h3>写信给管理员</h3>
</div>
<div class='panel-body'>
{{ form.as_p }}
</div>
<div class='panel-footer'>
<input type='submit' value='提交'>
</form>
</div>
</div>
</div>
{% endblock %}
运行验证
邮件这个 就先不弄了
page247之前
views.contact
def contact(request):
if request.method == 'POST':
form = forms.ContactForm(request.POST)
if form.is_valid():
message = "感谢您的来信."
else:
message = "请检查您输入的信息是否正确!"
else:
form = forms.ContactForm()
template = get_template('contact.html')
request_context = RequestContext(request)
request_context.push(locals())
html = template.render(request_context)
return HttpResponse(html)
contact.html
<!-- contact.html (ch08www project) -->
{% extends "base.html" %}
{% block title %}联络管理员{% endblock %}
{% block content %}
<div class='container'>
{% if message %}
<div class='alert alert-warning'>{{ message }}</div>
{{ user_name }} <br/>
{{ user_city }} <br/>
{{ user_school }} <br/>
{{ user_email }} <br/>
{{ user_message | linebreaks }} <br/>
{% endif %}
<div class='panel panel-primary'>
<div class='panel-heading'>
<form name='my form' action='.' method='POST'>
{% csrf_token %}
<h3>写信给管理员</h3>
</div>
<div class='panel-body'>
{{ form.as_p }}
</div>
<div class='panel-footer'>
<input type='submit' value='提交'>
</form>
</div>
</div>
</div>
{% endblock %}
整理的源码
django_ch0822www.tar
https://download.csdn.net/download/wowocpp/11759637