论坛首页 编程语言技术论坛

django程序三分钟快速入门

浏览 3691 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (5) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-18  

目录D:\newtest

第一步:
D:\newtest> manage.py startapp  contact
产生文件
__init__.py
models.py
views.py

第二步:
在contact目录中新建forms.py文件
forms.py内容如下

# -*- coding: UTF-8 -*-
from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField()
    email = forms.EmailField(required=False)
    message = forms.CharField()

第三步:setting.py INSTALLED_APPS 加入应用程序
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'newtest.wiki',
    'newtest.address',
    'newtest.classmate',
    'newtest.ajax',
    'newtest.contact',#这是新加的


第四步:
D:\newtest>python manage.py shell
>>> from address.forms import ContactForm
>>> f = ContactForm()
>>> print f
<tr><th><label for="id_subject">Subject:</label></th><td><input type="text" name="subject" id="id_subject" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>
>>> print f.as_ul()
<li><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></li>
<li><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></li>
<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>
>>> print f.as_p()
<p><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></p>
<p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>
>>> print f.as_d()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'ContactForm' object has no attribute 'as_d'
>>> print f.as_p()
<p><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></p>
<p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>
>>> print f['subject']
<input type="text" name="subject" id="id_subject" />
>>> print f['message']
<input type="text" name="message" id="id_message" />
>>> ^Z


D:\newtest> manage.py startapp  contact
Error: [Error 183] : 'D:\\newtest\\..\\newtest\\contact'

D:\newtest> manage.py startapp  contact

D:\newtest>python manage.py shell
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from contact.forms import ContactForm
>>> f = ContactForm()
>>> f
<contact.forms.ContactForm object at 0x0115FB90>
>>> print f  #或者print f.as_table()结果一样
<tr><th><label for="id_subject">Subject:</label></th><td><input type="text" name="subject" id="id_subject" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>
>>> print f.as_ul()
#注意到<li>没有
<li><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></li>
<li><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></li>
<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>
>>> print f.as_p()
#注意到<p>没有
<p><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></p>
<p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>
>>> print f['subject']
<input type="text" name="subject" id="id_subject" />
>>> print f['message']
<input type="text" name="message" id="id_message" />
>>> print f['email']
<input type="text" name="email" id="id_email" />
>>> f = ContactForm({'subject':'Hello','email':'admin@sohu.com','message':'this is a forms instance'})
>>> f.is_bound
True
>>> f.is_valid()
False
>>> f = ContactForm({'subject':'Hello','message':'Nice site'})
>>> f.is_valid()
False
>>> f['message'].errors
[u'This field is required.']
>>> f['subject'].errors
[]
>>> f['email'].errors
[]
>>> f.errors
{'message': [u'This field is required.']}

>>> f = ContactForm({'subject':'Hello','email':'admin@sohu.com','message':'this is a forms
>>> f.is_valid()
True
>>> f.cleaned_data
{'message': u'this is a forms instance', 'email': u'admin@sohu.com', 'subject': u'Hello'}

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics