首页 > 代码库 > 跟我一起Django - 01 安装和初始化
跟我一起Django - 01 安装和初始化
python环境
C:\Python27\Lib\site-packages>python --version
Python 2.7.5
安装setuptools
下载脚本:https://bootstrap.pypa.io/ez_setup.py
python ez_setup.py
django环境
下载:https://www.djangoproject.com/download/1.7/tarball/
解压缩后进入目录:
python setup.py install... ...... ...
测试下:
E:\>pythonPython 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import django;>>> print django.get_version();1.7>>>
构建一个blog
python C:\Python27\Lib\site-packages\Django-1.7-py2.7.egg\django\bin\django-admin.py startproject mysite
ganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ls -ltotal 1-rwxr-xr-x 1 ganiks.l Administ 249 Sep 11 13:42 manage.pydrwxr-xr-x 1 ganiks.l Administ 0 Sep 11 14:13 mysiteganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ls -l mysite/total 2-rw-r--r-- 1 ganiks.l Administ 0 Sep 11 13:42 __init__.py-rw-r--r-- 1 ganiks.l Administ 2042 Sep 11 13:42 settings.py-rw-r--r-- 1 ganiks.l Administ 275 Sep 11 13:42 urls.py-rw-r--r-- 1 ganiks.l Administ 387 Sep 11 13:42 wsgi.py
运行开发服务器
ganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ./manage.py runserverPerforming system checks...System check identified no issues (0 silenced).September 11, 2014 - 14:22:59Django version 1.7, using settings ‘mysite.settings‘Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.[11/Sep/2014 14:23:03] "GET / HTTP/1.1" 200 1759
创建blog app
ganiks.liu@MAMIS-GAIKS-LIU /E/mysite/mysite$ ../manage.py startapp blogganiks.liu@MAMIS-GAIKS-LIU /E/mysite/mysite$ gvim settings.pyganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ls -ltotal 19-rw-r--r-- 1 ganiks.l Administ 36864 Sep 11 15:04 db.sqlite3-rwxr-xr-x 1 ganiks.l Administ 249 Sep 11 13:42 manage.py-rw-r--r-- 1 ganiks.l Administ 312 Sep 11 14:42 models.pydrwxr-xr-x 1 ganiks.l Administ 0 Sep 11 15:04 mysite
注意,这里是在mysite/mysite
目录下执行的创建app操作,所以blog是mysite.blog
编辑mysite/settings.py
, 添加mysite.blog
# Application definitionINSTALLED_APPS = ( ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘mysite.blog‘,)
Django 采用
INSTALL_APPS
来决定系统不同部分的配置,包括自动化的admin应用
以及测试框架
创建你的Model
from django.db import models# Create your models here.#django.db.models.Model is the core partof the ORM system of Djangoclass BlogPost(models.Model): # Django will add an auto-increment, unique id title = models.CharField(max_length=150) body = models.TextField() timestamp = models.DataTimeField()
设置数据库
使用SQLite
SQLite非常适合做测试,甚至可以部署在没有大量并发写入的情况下。因为SQLite使用本地文件系统作为存储介质并且用原生的文件系统权限来做访问限制。
像主机、端口、用户和密码这些统统不需要了。
创建表
ganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ./manage.py syncdbOperations to perform: Apply all migrations: admin, contenttypes, auth, sessionsRunning migrations: No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won‘t be applied. Run ‘manage.py makemigrations‘ to make new migrations, and then re-run ‘manage.py migrate‘ to apply them. You have installed Django‘s auth system, and don‘t have any superusers defined.Would you like to create one now? (yes/no): yesUsername (leave blank to use ‘ganiks.liu‘): ganiksEmail address: ganiks@qq.comPassword:Password (again):Superuser created successfully.ganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ manage.py makemigrationsMigrations for ‘blog‘: 0001_initial.py: - Create model BlogPostganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ls -ltotal 19-rw-r--r-- 1 ganiks.l Administ 36864 Sep 11 15:07 db.sqlite3-rwxr-xr-x 1 ganiks.l Administ 249 Sep 11 13:42 manage.pydrwxr-xr-x 1 ganiks.l Administ 0 Sep 11 15:04 mysiteganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ manage.py migrate perations to perform: Apply all migrations: admin, blog, contenttypes, auth, sessionsRunning migrations: Applying blog.0001_initial... OK ganiks.liu@MAMIS-GAIKS-LIU /E/mysite$ ls -ltotal 19-rw-r--r-- 1 ganiks.l Administ 37888 Sep 11 15:09 db.sqlite3-rwxr-xr-x 1 ganiks.l Administ 249 Sep 11 13:42 manage.pydrwxr-xr-x 1 ganiks.l Administ 0 Sep 11 15:04 mysite
当执行
syncdb
命令时, Django会查找INSTALLED_APPS
中的每一个models.py
文件。
并为每一个都创建一个数据库表(多对多关系会有例外)
另外刚刚的过程中还创建了一个超级用户,这个在后面加入Django的自动admin时很方便。
You have installed Django‘s auth system, and don‘t have any superusers defined.Would you like to create one now? (yes/no): yesUsername (leave blank to use ‘ganiks.liu‘): ganiksEmail address: ganiks@qq.comPassword:Password (again):Superuser created successfully.
设置自动admin应用:
自动化的后台应用程序admin可以说是Django“皇冠上的明珠”, 任何对为Web应用创建简单的CURD接口感到厌倦的人来说,这是极大的福音,类似Yii的Gii。
由于自动admin不是Django的必要组件,因此你先要在
settings.py
文件中指定,就跟你刚刚指定mysite.blog
一样。
但是呢,其实我这次用的 Django1.7 版本中默认的已经启用了如下app
(我现在看的教程是老版本2008年的,这样可以发现好多Django的进化过程~)
INSTALLED_APPS = ( ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘mysite.blog‘,)
还有 urls.py也都配置好了admin路由
from django.conf.urls import patterns, include, urlfrom django.contrib import adminurlpatterns = patterns(‘‘, # Examples: # url(r‘^$‘, ‘mysite.views.home‘, name=‘home‘), # url(r‘^blog/‘, include(‘blog.urls‘)), url(r‘^admin/‘, include(admin.site.urls)),)
最后,你的应用程序要告诉Django要在admin窗口中显示哪一个model可以供编辑;要做到这一点,只要想默认的admin站点注册你的app model即可【添加1,添加2】
from django.db import models#添加1from django.contrib import admin# Create your models here.#django.db.models.Model is the core partof the ORM system of Djangoclass BlogPost(models.Model): # Django will add an auto-increment, unique id title = models.CharField(max_length=150) body = models.TextField() timestamp = models.DateTimeField()#添加2admin.site.register(BlogPost)
我的app(Blog posts)没有出现在 admin中的3个常见原因:
- 忘记向
admin.site.register
注册你的model类 models.py
有错误- 忘记在
settings.py
中的INSTALLED_APPS
中添加app
现在blog app已经创建完成,如何完善,参看下一篇。
转自:http://www.cnblogs.com/ganiks/p/3967994.html
跟我一起Django - 01 安装和初始化