django基本原理及运行(一)

Posted on Posted in django
Tips: 本文创建于2014年11月15日,已超过 2 年,内容或图片可能已经失效!

f2076672b9142eb301080776cf9ea1d7

1.创建一个项目

  1. django-admin.py?startproject?myweb

项目里有一下几个目录

  1. myweb/
  2. ├──?manage.py?????#django自带的服务器管理
  3. └──?myweb
  4. ????├──?__init__.py???#表明这是一个包结构的,这是一个模块
  5. ????├──?settings.py?#包含项目的所有配置参数
  6. ????├──?urls.py????????#URL根配置
  7. ????└──?wsgi.py???????#内置runserver命令的WSGI应用配置

2.更改项目里的基本配置
settings.py

  1. vim?myweb/myweb/settings.py
  2. TIME_ZONE?=?'Asia/Shanghai'????????#设置时区
  3. LANGUAGE_CODE?=?'zh-cn'????????????#设置语言类型
  4. INSTALLED_APPS?=?(
  5. ????'django.contrib.admin',
  6. ????'django.contrib.auth',
  7. ????'django.contrib.contenttypes',
  8. ????'django.contrib.sessions',
  9. ????'django.contrib.messages',
  10. ????'django.contrib.staticfiles',
  11. ????'blog',?????????????????????????????????????????#增加一个自己的app
  12. )

urls.py

  1. vim?myweb/myweb/urls.py
  2. from?django.conf.urls?import?patterns,?include,?url
  3. from?django.contrib?import?admin
  4. urlpatterns?=?patterns('',
  5. ????#?Examples:
  6. ????#?url(r'^$',?'myweb.views.home',?name='home'),
  7. ????#?url(r'^blog/',?include('blog.urls')),
  8. ????url(r'^admin/',?include(admin.site.urls)),
  9. ????????url(r'^blog/index/$','blog.views.index'),????#增加这句,表明访问blog/index的时候,调用blog中views里的index方法
  10. )

3.创建blog app

  1. cd?myweb
  2. django-admin.py?startapps?blog????#blog要与settings里面添加的blog要保持一致

blog的目录结构

  1. blog
  2. ├──?admin.py
  3. ├──?__init__.py
  4. ├──?migrations
  5. │???└──?__init__.py
  6. ├──?models.py
  7. ├──?tests.py
  8. └──?views.py

4.访问实例
修改blog里的views.py

  1. vim?blog/views.py
  2. from?django.http?import?HttpResponse
  3. def?index(req):
  4. ????????return?HttpResponse('<h1>hello</h1>')

5.运行manage.py

  1. python?manage.py?migrate????#如果提示未迁移,则运行这个命令,会有提示
  2. python?manage.py?runserver

python-manage

» 转载请注明来源:若我若鱼 » django基本原理及运行(一)

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen − nine =