Python新手使用的Django架站的16堂课 --- 第八章(四)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值