标签:django
1.HttpRequest对象
HttpRequest对象包含当前请求URL的一些信息:
有关request的其它信息:
request.META 是一个Python字典,包含了所有本次HTTP请求的Header信息,这个字典中几个常见的键值有
键值 | 作用 |
---|---|
HTTP_REFERER | 进站前链接网页,如果有的话 |
HTTP_USER_AGENT | 用户浏览器的user-agent字符串,如果有的话。 |
REMOTE_ADDR | 客户端IP,如:"12.345.67.89" 。(如果申请是经过代理服务器的话,那么它可能是以逗号分割的多个IP地址,如:"12.345.67.89,23.456.78.90" 。) |
写一个view显示request.META中的所有数据
# 显示 request.META 的所有数据 def display_meta(req): values = req.META.items() values.sort() html = [] for k, v in values: html.append(‘<tr><td>%s</td><td>%s</td></tr>‘ % (k, v)) return HttpResponse(‘<table>%s</table>‘ % ‘\n‘.join(html))
2. 类字典对象
request.GET和request.POST是类字典对象,意思是他们的行为像Python里标准的字典对象,但在技术底层上他们不是标准字典对象。因为request.GET和request.POST拥有一些普通的字典对象所没有的方法。
在视图中使用Form对象
创建文件夹:
在forms.py中添加代码:
from django import forms class ContactForm(forms.Form): subject = forms.CharField(max_length=100, label=‘subject‘) email = forms.EmailField(required=False, label=‘Your e-mail address‘) message = forms.CharField(widget=forms.Textarea, label=‘message‘) def clean_massage(self): message = self.cleaned_data[‘message‘] num_words = len(message.split()) if num_words < 4: raise forms.ValidationError("Not enough words") return message
在view.py中添加
from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from contact.forms import ContactForm def contact_f(req): if req.method == ‘POST‘: form = ContactForm(req.POST) if form.is_valid(): # cd = form.cleaned_data # print cd[‘subject‘] # print cd[‘email‘] # print cd[‘message‘] return HttpResponseRedirect(‘/thanks/‘) else: form = ContactForm() return render_to_response(‘contact_form.html‘, {‘form‘: form}) def thanks(req): return render_to_response(‘thanks.html‘)
在urls.py中添加
from contact.view import contact_f, thanks urlpatterns = patterns(‘‘, url(r‘^contact_f/$‘, contact_f), url(r‘^thanks/$‘, thanks), )
在templates中添加html文件
contact_form.py
<!DOCTYPE html> <html> <style type="text/css"> ul.errorlist{ margin: 0; padding: 0; } .errorlist li{ background-color: red; color: white; display: block; font-size: 10px; margin: 0 0 3px; padding: 4px 5px; } .field{ background-color: gray; } </style> <head lang="en"> <meta charset="UTF-8"> <title>Contact us</title> </head> <body> <h1>Contact us</h1> {% if form.errors %} <p style="color: red;"> Please correct the error{{ form.errors | pluralize }} below. </p> {% endif %} <form action="" method="post"> <div class="field"> {{ form.subject.errors }} <label for="id_subject">Subject:</label> {{ form.subject }} </div> <div class="field"> {{ form.email.errors }} <label for="id_email">Your e‐mail address:</label> {{ form.email }} </div> <div class="field"> {{ form.message.errors }} <label for="id_message">Message:</label> {{ form.message }} </div> <input type="submit" value="Submit"> </form> </body> </html>
thanks.py
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>Thanks!!!</h1> </body> </html>
笔记来自《Django中文教程》
标签:django
原文地址:http://4440271.blog.51cto.com/4430271/1656802