1.创建虚拟环境
2.安装django
3.创建项目
4.创建应用
5.在models.py中定义模型类
6.定义视图
7.配置url
8.创建模板
sudo pip install virtualenv
virtualenv venv
source venv/bin/activate
deactivate
rm -r venv
直接删除虚拟环境所在的文件夹venv就删除了我们创建的venv虚拟环境。
查看虚拟环境中已经安装的包
pip list
pip freeze
建议安装1.8.2版本,这是一个稳定性高、使用广、文档多的版本
pip install django==1.8.2
查看版本:进入python shell,运行如下代码
import django
django.get_version()
说明:使用pip install django命令进行安装时,会自动删除旧版本,再安装新版本
django-admin startproject 项目名
python manage.py startapp booktest
from django.db import models
class BookInfo(models.Model):
btitle = models.CharField(max_length=20)
bpub_date = models.DateTimeField()
def _ _str_ _(self):
return "%d" % self.pk
class HeroInfo(models.Model):
hname = models.CharField(max_length=20)
hgender = models.BooleanField()
hcontent = models.CharField(max_length=100)
hBook = models.ForeignKey('BookInfo')
def _ _str_ _(self):
return "%d" % self.pk
python manage.py makemigrations
python manage.py migrate
python manage.py shell
from booktest.models import BookInfo,HeroInfo
from django.utils import timezone
from datetime import *
BookInfo.objects.all()
b = BookInfo()
b.btitle="射雕英雄传"
b.bpub_date=datetime(year=1990,month=1,day=10)
b.save()
b=BookInfo.objects.get(pk=1)
b
b.id
b.btitle
b.btitle=u"天龙八部"
b.save()
b.delete()
h=HeroInfo()
h.htitle=u'郭靖'
h.hgender=True
h.hcontent=u'降龙十八掌'
h.hBook=b
h.save()
b.heroinfo_set.all()
h=b.heroinfo_set.create(htitle=u'黄蓉',hgender=False,hcontent=u'打狗棍法')
python manage.py runserver ip:port
python manage.py runserver 8080
python manage.py createsuperuser,按提示输入用户名、邮箱、密码
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
from django.contrib import admin
from models import BookInfo
admin.site.register(BookInfo)
class QuestionAdmin(admin.ModelAdmin):
...
admin.site.register(Question, QuestionAdmin)
list_display = ['pk', 'btitle', 'bpub_date']
list_filter = ['btitle']
search_fields = ['btitle']
list_per_page = 10
fields = ['bpub_date', 'btitle']
fieldsets = [
('basic',{'fields': ['btitle']}),
('more', {'fields': ['bpub_date']}),
]
from django.contrib import admin
from models import BookInfo,HeroInfo
class HeroInfoInline(admin.StackedInline):
model = HeroInfo
extra = 2
class BookInfoAdmin(admin.ModelAdmin):
inlines = [HeroInfoInline]
admin.site.register(BookInfo, BookInfoAdmin)
class HeroInfoInline(admin.TabularInline)
def gender(self):
if self.hgender:
return '男'
else:
return '女'
gender.short_description = '性别'
class HeroInfoAdmin(admin.ModelAdmin):
list_display = ['id', 'hname', 'gender', 'hcontent']
#coding:utf-8
from django.http import HttpResponse
def index(request):
return HttpResponse("index")
def detail(request,id):
return HttpResponse("detail %s" % id)
url(r'^', include('booktest.urls')),
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^([0-9]+)/$', views.detail),
]
'DIRS': [os.path.join(BASE_DIR, 'templates')],
{{输出值,可以是变量,也可以是对象.属性}}
{%执行代码段%}
<html>
<head>
<title>首页title>
head>
<body>
<h1>图书列表h1>
<ul>
{%for book in booklist%}
<li>
<a href="{{book.id}}">
{{book.btitle}}
a>
li>
{%endfor%}
ul>
body>
html>
在模板中访问对象成员时,都以属性的方式访问,即方法也不能加括号
<html>
<head>
<title>详细页title>
head>
<body>
<h1>{{book.btitle}}h1>
<ul>
{%for hero in book.heroinfo_set.all%}
<li>{{hero.hname}}---{{hero.hcontent}}li>
{%endfor%}
ul>
body>
html>
from django.http import HttpResponse
from django.template import RequestContext, loader
from models import BookInfo
def index(request):
booklist = BookInfo.objects.all()
template = loader.get_template('booktest/index.html')
context = RequestContext(request, {'booklist': booklist})
return HttpResponse(template.render(context))
def detail(reqeust, id):
book = BookInfo.objects.get(pk=id)
template = loader.get_template('booktest/detail.html')
context = RequestContext(reqeust, {'book': book})
return HttpResponse(template.render(context))
<a href="{{book.id}}">
url(r'^book/([0-9]+)/$', views.detail),
url(r'^admin/', include(admin.site.urls, namespace='booktest')),
url(r'^book/([0-9]+)/$', views.detail, name="detail"),
<a href="{%url 'booktest:detail' book.id%}">
from django.shortcuts import render
from models import BookInfo
def index(reqeust):
booklist = BookInfo.objects.all()
return render(reqeust, 'booktest/index.html', {'booklist': booklist})
def detail(reqeust, id):
book = BookInfo.objects.get(pk=id)
return render(reqeust, 'booktest/detail.html', {'book': book})