代码链接://download.csdn.net/download/u014651560/11979439
1.1首先自己安装把环境安装好,安装python,然后再命令行里输入:
$ pip install Django
$ pip install django-simpleui
1.2 在某个文件夹下,打开命令窗口
python startproject test1
1.3 可以直接运行
$ python manage.py runserver
1.4 cd test1 创建booktest应用
python manage.py startapp booktest
1.5 创建应用后需要在settings.py 文件中 install_app 添加应用
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'booktest',
'simpleui'
)
1. 6 写在booktest/model.py文件中,写入
from django.db import models
# Create your models here.
class BookInfo(models.Model):
title=models.CharField(max_length=20)
pub_date=models.DateField()
def __str__(self):
return self.title.encode('utf-8')
class HeroInfo(models.Model):
name=models.CharField(max_length=10)
content=models.CharField(max_length=100)
gender=models.BooleanField(default=True)
book=models.ForeignKey(BookInfo)
def __str__(self):
return self.name.encode('utf-8')
1. 7 使用sqlsite生成迁移文件
python manage.py makemigrations
1. 8 根据迁移文件去创建数据库表结构
python manage.py migrate
1. 9 此处可以进行数据库操作
python manage.py shell
from booktest.models import BookInfo
BookInfo.object.all() #查询
b = BookInfo()
b.title = '哈哈'
from datetime import date
b.pub_date=date(2017,1,1)
b.save()
b=BookInfo.object.get(id=1)
t.title='hujun'
b.save()
b.delete()
from booktest.models import HeroInfo
b=HeroInfo()
b.name='郭靖'
b.gender=true
b.content='降龙'
b.book=BookInfo.object.get(id=2)
b.save()
1. 10 后台管理,创建超级管理员
python manage.py createsuperuser
1. 11 在应用booktest的admin.py文件中进行注册
from django.contrib import admin
from .models import *
# Register your models here.
class BookInfoAdmin(admin.ModelAdmin):
list_display = ['id','title','pub_date']
class HeroInfoAdmin(admin.ModelAdmin):
list_display = ['id','name','content','gender','book']
admin.site.register(BookInfo,BookInfoAdmin)
admin.site.register(HeroInfo,HeroInfoAdmin)
1. 12 在应用booktest的views.py文件中进行视图编写
#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
#HttpRequest
def index(request):
#return HttpResponse('<h1>Hello World</h1>')
return render(request,'booktest/index.html')
1. 13 在根目录下test1下的urls.py进行配置url ,加一条
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('^',include('booktest.urls')),
]
1. 14 在应用booktest目录下添加一个urls.py文件
from django.conf.urls import url
from . import views
urlpatterns = [
url('^$',views.index),
url('^(\d+)/$',views.detail),
]
1. 15 在跟目录下新建一个template,和manage.py在同一级目录,然后在template目录再新建一个booktest目录,再在booktest目录下新建index.html,即test1/template/booktest/index.html
将views.py文件中的方法进行改写
#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
#HttpRequest
def index(request):
#return HttpResponse('<h1>Hello World</h1>')
return render(request,'booktest/index.html')
1. 16 为了能够找到template文件夹,需要在settings.py文件中进行添加
TEMPLATES=[
'DIRS':[os.path.join(BASE_DIR, 'templates')],
]
1. 17 在应用booktest的views.py文件中进行视图编写
#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
#HttpRequest
def index(request):
#HttpResponse
# return HttpResponse('<h1>hello world</h1>')
# context={'title':'django首页','list':range(10)}
# return render(request,'booktest/index.html',context)
list=BookInfo.objects.all()
context={'booklist':list}
return render(request,'booktest/index2.html',context)
def detail(request,id):
list=BookInfo.objects.get(id=id).heroinfo_set.all()
context={'herolist':list}
return render(request,'booktest/detail.html',context)
1. 18 新建index2.html,在body写入
<ul>
{% for book in booklist%}
<l1><a href="/{{book.id}}"></a>{{book.title}}</l1>
{% endfor%}
</ul>
1. 19 在应用booktest的urls.py文件中添加
from django.conf.urls import url
from . import views
urlpatterns = [
url('^$',views.index),
url('^(\d+)/$',views.detail),
]
1. 20 在template/booktest的目录下新建detail.html
<ul>
{% for hero in herolist%}
<l1>{{hero.name}}</l1>
{% endfor%}
</ul>
注意: 改变数据库为mysql后,需要告诉django用pymysql替换它默认的mysql_db模块连接数据库,因为mysql_db现在已经没用了,所以需要替换模块。在项目文件夹下的__init__.py或者是应用文件夹下的__init__.py中加入以下语句。
import pymysql
pymysql.install_as_MySQLdb()