Diango 安装与入门示例
Diango 安装指导
1. 前提
安装了 Python
Django要求 Python 版本 2.4 to 2.7.
推荐 2.5以上,它包含轻量级的数据库SQLite.
目前推荐使用2.6版本,2.7的第三方包很多不能使用.开发工具:
Pydev
www.pydev.org
安装配置:
python2.6 + django1.2.0 +eclipse 3.6 + pydev 1.6.0
注意插件版本, 不然会出问题的.
Python 2.7 for Windows
http://www.python.org/ftp/python/2.7.1/python-2.7.1.msi
Django 官方版本
http://media.djangoproject.com/releases/1.2/Django-1.2.4.tar.gz
2. 安装 Python 2.7
直接安装即可,安装目录E:\Python217.
配置环境变量:
PATH
加入目录 E:\Python217
新建PYTHONPATH
加入目录
E:\Python313\pyfiles,此目录用于创建你自己的*.py文件.
3. 安装 Django 1.2.4
解压Django-1.2.4.tar.gz到E:\
CMD进入目录E:\Django-1.2.4\
CMD> setup.py install
注意:
3.1 如果提示
if u'SVN' in version
语法错误
把u去掉即可,类似错误同样处理.
这主要是因为解释器不能识别这种unicode字符串语法.
3.2 如果安装了更高版本的Python,请先卸载.卸载后不用重启.
因为Django可能安装到高版本.
4. 验证
4.1 检查目录:\Python217\Lib\site-packages下是否有diagno目录.site-packages目录是第三方包存放的目录.
4.2 CMD>python
>>>import django
>>>django.get_version()
显示:1.2.4
>>>
5. 安装完成.
6. 示例
6.1 创建项目文件夹:
E:\python_projects
6.2 创建项目mysite
6.2.1 配置环境变量PATH
加入
E:\Python217\Lib\site-packages\django\bin
6.2.2 CMD进入E:\python_projects.
> django-admin.py startproject mysite
在E:\python_projects\mysite目录下会生成4个文件
__init__.py
manage.py
settings.py
urls.py
__init__.py: 一个空文件,标识mysite是一个Python包.
manage.py: 一个与Django交互的命令行工具.
settings.py: 项目配置文件.
urls.py: URL的声明,使用正则表达式.
6.3 启动自带的纯Python语言实现的开发者WEB服务器
6.3.1 CMD进入mysite目录
> manage.py runserver
E:\python_projects\mysite>manage.py runserverValidating models...0 errors foundDjango version 1.2.4, using settings 'mysite.settings'Development server is running at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'E:/python_projects/mysite/db/test.db', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', }}E:\python_projects\mysite>manage.py syncdbCreating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_user_permissionsCreating table auth_user_groupsCreating table auth_userCreating table auth_messageCreating 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 'cs'): adminE-mail address: amos_tl@126.comPassword:Password (again):Superuser created successfully.Installing index for auth.Permission modelInstalling index for auth.Group_permissions modelInstalling index for auth.User_user_permissions modelInstalling index for auth.User_groups modelInstalling index for auth.Message modelNo fixtures found.E:\python_projects\mysite>
from django.db import modelsclass Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(max_length=200) votes = models.IntegerField()BEGIN;CREATE TABLE "polls_poll" ( "id" integer NOT NULL PRIMARY KEY, "question" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);CREATE TABLE "polls_choice" ( "id" integer NOT NULL PRIMARY KEY, "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"), "choice" varchar(200) NOT NULL, "votes" integer NOT NULL);COMMIT;>>> from polls.models import Poll, Choice>>> Poll.objects.all()[]>>> import datetime>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())>>> p.save()>>> p.id1>>> p.question"What's up?">>> p.pub_datedatetime.datetime(2007, 7, 15, 12, 00, 53)>>> p.pub_date = datetime.datetime(2007, 4, 1, 0, 0)>>> p.save()>>> Poll.objects.all()[<Poll: Poll object>]>>> quit()
from django.conf.urls.defaults import *# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()urlpatterns = patterns('', # Example: # (r'^mysite/', include('mysite.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: (r'^admin/', include(admin.site.urls)),)from polls.models import Pollfrom django.contrib import adminadmin.site.register(Poll)
{% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> {% endfor %} </ul>{% else %} <p>No polls are available.</p>{% endif %}