一般创建网站的步骤:
终端里面输入:
1. django-admin.py startproject mysite (最后一个为创建的项目的名称。)
2. 开启服务 python manage.py runserver (默认是8000端口,要cd manage.py 的目录)
连接数据库:
2. 运行python manage.py syncdb 保存刚才的变动
新建一个app:
1. python manage.py startapp name
2. 编辑name下面的model.py文件 创建需要的model (及创建相关的类)
3. 在settings.py 的 INSTALLED_APPS 里加入 'name', (激活模块)
4. 保存python manage.py syncdb
Play with the API
1.run python manage.py shell
创建admin后台:
1. 设置setting里面的INSTALLED——APPS
2. python manage.py syncdb
3. 编辑mysite下面的urls.py 撤销关于admin的注释
4. python manage.py runserver
5. 如果要是自己写的app在admin权限下,就要在app下面创建一个admin.py
创建url链接:
1. 在新建的app下面新建一个urls.py 文件
2. 在mysite文件下编辑urls.py 增加一个路径 eg :url(r’^polls/’, include(’polls.urls’)),
3. 在app的下面的url下面编辑urls.py 增加自己的url
每次创建一个新的网页就是创建一个新的类,要让类能够被访问的就需要在urls.py文件里面加url定位的描述
django 的一些基本理念:
1. 整个动态网站分为3层 :数据处理(models.py)、视图(views.py)、模板(templates/xx.html)
自己激活的app下面的admin.py一般要包括以下代码:
from django.contrib import admin
from polls.models import Poll
admin.site.register(Poll)
附录:
If you’re interested, also run the following commands:
• python manage.py validate – Checks for any errors in the construction of your models.
• python manage.py sqlcustom polls – Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
• python manage.py sqlclear polls – Outputs the necessary DROP TABLE statements for this app,according to which tables already exist in your database (if any).
• python manage.py sqlindexes polls – Outputs the CREATE INDEX statements for this app.
• python manage.py sqlall polls – A combination of all the SQL from the sql, sqlcustom, and sqlindexes commands.