Linux环境下
在python3中,创建虚拟环境
mkvirtualenv -p python3 虚拟机名称
mkvirtualenv -p python3 py_django
查看创建的虚拟环境
workon
退出当前的虚拟环境
deactivate
删除虚拟环境(不要做)
rmvirtualenv 虚拟机名称
rmvirtualenv py_django
进入/切换到虚拟环境
workon py_django
下载:本文用的是1.8.2版本
pip install django==1.8.2
查看下载包
pip list
django-admin startproject BookManger
创建应用
python manage.py startapp Book
用pycharm打开BookManger
配置Project Interpreter
运行项目
python manage.py runserver
class BookInfo(models.Model):'''定义书籍信息模型类'''# 设计name属性name = models.CharField(max_length=10)def __str__(self):'''以字符串的形式输出模型类'''return self.nameclass PeopleInfo(models.Model):'''定义人物信息模型类'''name = models.CharField(max_length=10)gender = models.BooleanField()book = models.ForeignKey(BookInfo)def __str__(self):'''以字符串的形式输出模型类'''return self.name
python manage.py makemigrations
python manage.py migrate
#语言
LANGUAGE_CODE = 'zh-Hans' #'en-us'#时区
TIME_ZONE = 'Asia/Shanghai' #'UTC'
python manage.py createsuperuser
from django.contrib import admin
from Book.models import BookInfo, PeopleInfo
# Register your models here.class PeopleInfoAdmin(admin.ModelAdmin):'''人物信息模型类的站点管理类'''list_display = ['id','name','gender','book']# 注册书籍信息模型类
admin.site.register(BookInfo)
# 注册人物信息模型类
admin.site.register(PeopleInfo, PeopleInfoAdmin)
from django.shortcuts import render
from django.http import HttpResponse
from Book.models import BookInfo, PeopleInfo
# Create your views here.def peopleList(request,book_id):'''提供人物信息'''# 获取book_id:通过正则的组取值,并自动传入# 查询出用户查询的那本书book = BookInfo.objects.get(id=book_id)# 查询出书里面的人物信息people_list={PeopleInfo,PeopleInfo}people_list = book.peopleinfo_set.all()# 上下文context = {'people_list':people_list}# 调出模板后渲染return render(request,'Book/peopleList.html',context)def bookList(request):'''提供书籍信息界面'''# 查询出所有书籍信息 book_list = {BookInfo,BokInfo}book_list = BookInfo.objects.all()context = {'book_list':book_list}return render(request,'Book/bookList.html',context)def test(request):'''测试:请求到视图的逻辑'''# 没有使用模板# return HttpResponse('测试')# 调用模板并相应:未接收参数# return render(request,'Book/test.html')# 上下文:是个字典,一般封装从数据库中查询出来的数据context = {'test':'测试'}# 调用模板并相应:接收上下文参数return render(request,'Book/test.html',context)
from django.conf.urls import url
from Book.views import test, bookList,peopleListurlpatterns = [# http://127.0.0.1:8000/test/# 正则匹配,如果匹配成功,直接调用指定的视图url(r'^test/$',test),# http://127.0.0.1:8000/booklist/url(r'^booklist/$',bookList),url(r'^(\d+)/$',peopleList)
]
from django.conf.urls import include, url
from django.contrib import adminurlpatterns = [# http://127.0.0.1:8000/admin/# 正则匹配,对请求地址进行正则匹配,如果路径中包含‘admin/’,就把后台站点中的urls信息包含到项目中,指明下一级路径如何匹配url(r'^admin/', include(admin.site.urls)),# http://127.0.0.1:8000/test/# 正则匹配,对请求地址进行正则匹配,如果路径中不包含‘admin/’,就把后台站点中的urls信息包含到项目中,指明下一级路径如何匹配url(r'^',include('Book.urls'))
]
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>书籍信息</title>
</head>
<body>
<ul>{% for book in book_list %}<li><a href="/{{ book.id }}/">{{ book.name }}</a></li>{% endfor %}
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>人物列表信息</title>
</head>
<body>
<ul>{% for people in people_list %}<li>{{ people.name }}</li>{% endfor %}
</ul>
</body>
</html>
完成。如下图: