Python Version: 2.7.10
Django Version: 1.9
REST framework 3.3.2
创建项目:
django-admin startproject TestWeb
切换到项目中创建应用:
django-admin startapp app01
修改配置文件settings.py,添加你的应用app01和rest_framework
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01'
'rest_framework',
]
app01文件夹下创建templates文件夹放入index.html模板文件
告诉你的应用模板文件路径,修改settings.py
TEMPLATE_DIRS=os.path.join(os.path.dirname(__file__),'templates')
最新版1.9已无需配置默认会到应用的templates文件夹下寻找
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
配置urls.py文件
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', index),
]
启动你的应用
python manage.py runserver 8080
后台管理
数据库配置默认是sqlite,mysql的配置如下
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'USER':'root',
'PASSWORD':'',
'NAME':'test',
'HOST':'localhost',
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
localhost:8080/admin/登录报no such table: auth_user错误
运行python manage.py syncdb即可自动创建所需表
注意:Django 1.7.1及以上的版本python manage.py syncdb失效需要用以下命令
python manage.py makemigrations
python manage.py migrate
创建模型,修改models.py
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Mysite(models.Model):
title=models.CharField(max_length=100)
url=models.URLField()
author=models.CharField(max_length=100)
num=models.IntegerField()
def __unicode__(self):
return self.title
class Meta: #查询时会以num排序
ordering= ['num']
python manage.py shell 进入交互式数据查询
from app01.models import *
m=Mysite(title='django',num=2)
m.save()
m=Mysite.objects.all()
m[0].title
m=Mysite.objects.get(num=2)
m=Mysite(title='django',num=3)
m.save()
m=Mysite(title='django',num=9)
m.save()
m=Mysite.objects.all().order_by('num')#升序
m=Mysite.objects.all().order_by('-num')#降序
m.delete()
m=Mysite.objects.all()[0:2]
管理界面管理你的模型admin.py
from django.contrib import admin
from app01.models import *
# Register your models here.
admin.site.register(Mysite)
使用表单出现CSRF verification failed. Request aborted.错误
解决方法:
[http://www.91pen.net/slove-the-problem-when-the-django-form-submitted-occur-403-error-csrf-verification-failed-request-aborted.html](http://www.91pen.net/slove-the-problem-when-the-django-form-submitted-occur-403-error-csrf-verification-failed-request-aborted.html)
urls.py配置
from django.conf.urls import url
from django.contrib import admin
from app01.views import *
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', index),
url(r'^hello/$', hello),
]
静态资源文件配置
STATIC_URL = '/static/' STATICFILES_DIRS=( 'D:/TestWeb/static', )
参考文章:
[http://blog.laisky.com/p/django-rest/](http://blog.laisky.com/p/django-rest/)
[http://www.ziqiangxuetang.com/django/django-static-files.html](http://www.ziqiangxuetang.com/django/django-static-files.html)