django-->python web framework (小菜鸟篇)

django自带web server, 故django开发的项目可以独立的运行,也可以安置在apache(+mod_python)下运行

django wiki

django主页

hello,word demo


django的官网手册 http://www.djangobook.com/en/2.0/; 对应的中文翻译版本 http://download.csdn.net/detail/xiarendeniao/4232144http://djangobook.py3k.cn/


下面是小菜鸟篇:

vpython ~/venv/bin/django-admin.py startproject mysite
vpython manage.py runserver 8001

访问出现django1.jpg

django-->python web framework (小菜鸟篇)_第1张图片

设置setting.py中的数据库、时区

[dongsong@bogon mysite]$ !vpy
vpython manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'dongsong'): dongsong
E-mail address: [email protected]     
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

创建app子目录

[dongsong@bogon mysite]$ vpython manage.py startapp polls
[dongsong@bogon mysite]$ ls 
manage.py  mysite  polls

修改polls/model.py,添加数据表的结构
在setting.py的INSTALLED_APPS中添加polls


显示建表语句

[dongsong@bogon mysite]$ vpython manage.py sql polls
BEGIN;
CREATE TABLE `polls_poll` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `question` varchar(200) NOT NULL,
    `pub_date` datetime NOT NULL
)
;
CREATE TABLE `polls_choice` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `poll_id` integer NOT NULL,
    `choice` varchar(200) NOT NULL,
    `votes` integer NOT NULL
)
;
ALTER TABLE `polls_choice` ADD CONSTRAINT `poll_id_refs_id_a27693dd` FOREIGN KEY (`poll_id`) REFERENCES `polls_poll` (`id`);
COMMIT;

创建数据表

[dongsong@bogon mysite]$ vpython manage.py syncdb
Creating tables ...
Creating table polls_poll
Creating table polls_choice
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

django交互模式

[dongsong@bogon mysite]$ vpython manage.py shell
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from polls.models import Poll,Choice
>>> Poll.objects.all()
[]
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now())
>>> p
<Poll: Poll object>
>>> p.save()
>>> p.id
1L
>>> p.question
"What's new?"
>>> p.pub_date
datetime.datetime(2012, 4, 15, 4, 4, 54, 856916, tzinfo=<UTC>)
>>> 
>>> p.question = "What's up?"
>>> p.save()
>>> Poll.objects.all()
[<Poll: What's up?>]

修改urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    url(r'^admin/',include(admin.site.urls)),
)
同时把setting.py中django.contrib.admin注释拿掉


运行,访问出现django2.jpg

django-->python web framework (小菜鸟篇)_第2张图片


设置urls.py

url(r'^polls/$','polls.views.index'),

修改polls/views.py

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

运行,访问出现django3.jpg

django-->python web framework (小菜鸟篇)_第3张图片

你可能感兴趣的:(django-->python web framework (小菜鸟篇))