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

来源:互联网 发布:php 数据库 主机配置 编辑:程序博客网 时间:2024/04/29 02:55
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

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

[dongsong@bogon mysite]$ !vpyvpython manage.py syncdbCreating tables ...Creating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_user_permissionsCreating table auth_user_groupsCreating table auth_userCreating table django_content_typeCreating table django_sessionCreating table django_siteYou just installed Django's auth system, which means you don't have any superusers defined.Would you like to create one now? (yes/no): yesUsername (leave blank to use 'dongsong'): dongsongE-mail address: xudongsong0102@163.com     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 pollsBEGIN;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 syncdbCreating tables ...Creating table polls_pollCreating table polls_choiceInstalling custom SQL ...Installing indexes ...Installed 0 object(s) from 0 fixture(s)

django交互模式

[dongsong@bogon mysite]$ vpython manage.py shellPython 2.6.6 (r266:84292, Dec  7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "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.id1L>>> p.question"What's new?">>> p.pub_datedatetime.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, urlfrom django.contrib import adminadmin.autodiscover()urlpatterns = patterns('',    url(r'^admin/',include(admin.site.urls)),)
同时把setting.py中django.contrib.admin注释拿掉


运行,访问出现django2.jpg


设置urls.py

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

修改polls/views.py

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

运行,访问出现django3.jpg