django实例:一创建一个应用头票系统
这个投票系统的主要功能有:
1)一个前台页面,可以让用户来投票
2)一个管理员页面,可以用来添加,修改,删除投票
安转django
解压原码包,之后执行
python setup.py install
创建项目
首页在命令窗口中,用cd 进入到你想要放置代码的目录,然后执行如下命令创建项目
django-admin.py startproject mysite
创建的项目目录结构如
[root@yangguoqiang Django-1.5.12]# ls -R mysite/
mysite/:
manage.py mysite polls
mysite/mysite:
__init__.py __init__.pyc settings.py settings.pyc urls.py urls.pyc wsgi.py wsgi.pyc
mysite/polls:
admin.py __init__.py models.py templates urls.py views.py
admin.pyc __init__.pyc models.pyc tests.py urls.pyc views.pyc
面再说说这几个目录和文件吧
1、根目录mysite:这个主要是用来存放生成的项目代码的,和项目没有关系,这个目录的名字你可以随便重命名
2、manage.py:这个里面都是一些命令,是用来对项目进行一些常用的操作,比如创建项目,创建应用,运行项目等
3、里面的mysite目录:这个就是项目包了,目录的名字就是项目包的名字,要用这个名字来import你需要的东西。
3、mysite/init.py:这个文件主要是告诉python当前的mysite目录是一个包,把mysite目录当然包来处理,相当于c#的命名空间。
4、mysite/settings.py:一些python项目的配置东西
5、mysite/urls.py:访问网站url时的相对应的处理模块。
6、mysite/wsgi.py:告诉WSGI-compatible webservers,从这里启动你网站
启动服务
python manage.py runserver 0.0.0.0:8000 &
当然默认端口是8000,如果你的8000端口被人占用了,可以在runserver后面直接加上端口。