django入门-自己总结

总结

  1. 安装配置django运行的环境
  2. 编写模型,使用简单API与数据库交互
  3. 使用django的后台管理中维护数据
  4. 通过视图接收请求,通过模型获取数据,展示出来
  5. 调用模板完成展示

 

一、创建虚拟环境

C:\WINDOWS\system32>E:

E:\>cd E:\python_practice_ku

E:\python_practice_ku>virtualenv django_ttsx

E:\python_practice_ku>cd E:\python_practice_ku\django_ttsx\Scripts

E:\python_practice_ku\django_ttsx\Scripts>activate     

(django_ttsx) E:\python_practice_ku\django_ttsx\Scripts>cd ..

安装django:

(django_ttsx) E:\python_practice_ku\django_ttsx>pip install django

创建项目:

(django_ttsx) E:\python_practice_ku\django_ttsx>django-admin startproject ttsx

创建应用:

(django_ttsx) E:\python_practice_ku\django_ttsx>cd E:\python_practice_ku\django_ttsx\ttsx

(django_ttsx) E:\python_practice_ku\django_ttsx\ttsx>python manage.py startapp booktest

在项目工程settings.py对新生成的子应用进行注册:

 

二、编写模型,数据迁移

https://www.cnblogs.com/cherry-ning/articles/12219888.html  使用mysql数据库

 1 from django.db import models
 2 
 3 # Create your models here.
 4 
 5 class BookInfo(models.Model):
 6     btitle=models.CharField(max_length=20)
 7     bpub_date=models.DateTimeField()
 8     def __str__(self):
 9         return self.btitle
10 
11 class HeroInfo(models.Model):
12     hname=models.CharField(max_length=10)
13     hgender=models.BooleanField()
14     hcontent=models.CharField(max_length=1000)
15     hbook=models.ForeignKey(BookInfo,on_delete=models.CASCADE)
16     def __str__(self):
17         return self.hname

数据库迁移:

(django_ttsx) E:\python_practice_ku\django_ttsx\ttsx>python manage.py makemigrations

(django_ttsx) E:\python_practice_ku\django_ttsx\ttsx>python manage.py migrate

 

三、使用django的后台管理中维护数据

开启服务器:

(django_ttsx) E:\python_practice_ku\django_ttsx\ttsx>python manage.py runserver

创建管理员用户:

(django_ttsx) E:\python_practice_ku\django_ttsx\ttsx>python manage.py createsuperuser

管理界面本地化:

1)编辑项目工程settings.py文件如下:

 1 from django.utils.translation import gettext_lazy as _
 2 LANGUAGES = [
 3     ('zh-Hans', _('Chinese')),
 4 ]
 5 
 6 LANGUAGE_CODE = 'zh-Hans' #汉化
 7 
 8 TIME_ZONE = 'Asia/Shanghai' #时区
 9 
10 USE_I18N = True
11 
12 USE_L10N = True
13 
14 USE_TZ = True

2)向admin注册booktest的模型,打开booktest/admin.py文件,注册模型

 1 from django.contrib import admin
 2 from .models import *
 3 
 4 # Register your models here.
 5 class HeroInfoInline(admin.TabularInline):   #关联注册
 6     model = HeroInfo
 7     extra = 2
 8 
 9 class BookInfoAdmin(admin.ModelAdmin):
10     list_display = ['id','btitle','bpub_date']    #显示字段,可以点击列头进行排序
11     list_filter = ['btitle']      #过滤字段,过滤框会出现在右侧
12     search_fields = ['btitle']     #搜索字段,搜索框会出现在上侧
13     list_per_page = 10              #分页,分页框会出现在下侧
14     fieldsets = [              #fieldsets:属性分组
15         ('basic',{'fields':['btitle']}),
16         ('more',{'fields':['bpub_date']})
17     ]
18     inlines = [HeroInfoInline]
19 
20 admin.site.register(BookInfo,BookInfoAdmin)
21 admin.site.register(HeroInfo)

 

四、通过视图接收请求,通过模型获取数据,展示出来

定义views.py文件

 1 from django.shortcuts import render
 2 from django.http import *
 3 from django.template import RequestContext,loader
 4 from .models import *
 5 
 6 # Create your views here.
 7 
 8 def index(request):
 9     # temp=loader.get_template('booktest/index.html')  #加载模板
10     # return HttpResponse(temp.render())   #将模板渲染出来
11     booklist =BookInfo.objects.all()
12     context={'list':booklist}
13     return render(request,'booktest/index.html',context)   #可以使用这一句代替上面注释的两行代码,即此行代码就是封装了上面两行代码
14 
15 def detail(request,id):
16     book=BookInfo.objects.get(pk=id)  #获取某本书
17     herolist=book.heroinfo_set.all   #获取某本书的所有英雄名
18     context={'herolist':herolist}
19     return render(request,'booktest/detail.html',context)

在tttsx/urls.py插入booktest,使主urlconf连接到booktest.urls模块

1 from django.contrib import admin
2 from django.urls import path
3 from django.conf.urls import include,url
4 
5 urlpatterns = [
6     path('admin/', admin.site.urls),
7     url(r'^',include('booktest.urls'))  #正则表达式匹配url,当匹配^后,链接到booktest.urls模块
8 ]

在booktest中的urls.py中添加urlconf

1 from django.conf.urls import include,url
2 from . import views
3 
4 urlpatterns=[
5     url(r'^$',views.index), #当匹配成功后,调用views.index视图
6     url(r'^(\d+)$',views.detail)  #(\d+)即取id值
7 ]

还需在settings.py文件修改路径:

 

五、调用模板完成展示

定义index.html模板

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport"
 6           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8     <title>list</title>
 9 </head>
10 <body>
11 
12     <ul>
13        {% for book in list %}
14         <li>
15             <a href="{{book.id}}">{{book.btitle}}</a>
16         </li>
17         {%endfor%}
18     </ul>
19 </body>
20 </html>

定义detail.html模板

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport"
 6           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8     <title>herolist</title>
 9 </head>
10 <body>
11     <ul>
12         {%for hero in herolist%}
13         <li>{{hero.hname}}</li>
14         {%endfor%}
15     </ul>
16 </body>
17 </html>

 

 

posted on 2020-01-18 11:48  cherry_ning  阅读(137)  评论(0)    收藏  举报

导航