6章 Models
傳統(tǒng)的MVC結(jié)構(gòu)中,有模型這么一個概念。Django中,Models又是怎么一回事呢?
?
剛才生成的這些亂七八糟的數(shù)據(jù)遷移就是Django自帶的一些應(yīng)用
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',創(chuàng)建的數(shù)據(jù)遷移
?執(zhí)行python manage.py makemigrations,Django會在app/migrations/目錄下生成移植文件blog\migrations\0001_initial.py和blog2\migrations\0001_initial.py
?因為剛剛我們創(chuàng)建的那個模型,我們沒有給它添加主鍵。于是Django呢幫我們創(chuàng)建了這么一個字段用來當(dāng)做我們這個模型的主鍵。所以呢如果我們自己手動添加主鍵,這個id呢也就不會自動生成了。這個id還是挺好用的。
# Generated by Django 2.0.5 on 2018-05-18 14:07from django.db import migrations, modelsclass Migration(migrations.Migration):initial = Truedependencies = []operations = [migrations.CreateModel(name='Article',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('title', models.CharField(default='Title', max_length=32)),('content', models.TextField(null=True)),],),] # Generated by Django 2.0.5 on 2018-05-18 14:07from django.db import migrations, modelsclass Migration(migrations.Migration):initial = Truedependencies = []operations = [migrations.CreateModel(name='Article',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('title', models.CharField(default='Title', max_length=32)),('content', models.TextField(null=True)),],),]生成的數(shù)據(jù)表的SQL語句是可以看的。
這個文件id就是0001或者是0002這些。
使用Django默認(rèn)的sqlite3數(shù)據(jù)庫
這個就是我們的數(shù)據(jù)庫sqlite3
大部分都是Django自動生成的數(shù)據(jù)表。前面的blog前綴是Django自動添加的。rowid是這個軟件自動加的一個東西。第二個id就是我們的主鍵。
?
Django的模板語言不但支持字符串之類的那種傳統(tǒng)的數(shù)據(jù)傳遞,也支持這種對象的傳遞。
blog\views.py
# -*- coding: utf-8 -*- from __future__ import unicode_literalsfrom django.shortcuts import render# Create your views here.#from django.shortcuts import render from django.http import HttpResponse from . import models def index(request):#return HttpResponse('Hello, world!');#return render(request,'index.html',{'hello': 'Hello,Blog'})article = models.Article.objects.get(pk=1)#return render(request, 'blog/index.html')return render(request, 'blog/index.html',{'article':article})blog2\views.py
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render# Create your views here. from . import models def index(request):#return render(request, 'blog2/index.html')article = models.Article.objects.get(pk=1)return render(request, 'blog2/index.html',{'article':article})后臺代碼我們就寫完了
剛才傳遞了這么一個對象article到前端模板,而在模板中調(diào)用實例成員的方法和后臺是一樣的,就用這個.操作符。
blog\index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <!-- <h1>Hello,Blog</h1> --> <!-- <h1>Hello,Blog! </h1> --> <h1>{{ article.title }}</h1> <h3>{{ article.content }}</h3> </body> </html>blog2\index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <!-- <h1>Hello,Blog</h1> --> <!-- <h1>Hello,Blog2!</h1> --> <h1>{{ article.title }}</h1> <h3>{{ article.content }}</h3> </body> </html>這個就是Django的模型models的一些基本用法。
轉(zhuǎn)載于:https://www.cnblogs.com/ZHONGZHENHUA/p/9058081.html
總結(jié)
- 上一篇: 使用adb命令启动APK方法
- 下一篇: PID与伺服驱动器