1 2 3 4 5 6 7 8 9 10 11 12 13 | from django.db import models # Create your models here. class video(models.Model): name = models.CharField(max_length=50, null=False) width = models.IntegerField(default = 1280) height = models.IntegerField(default = 720) src = models.CharField(max_length=50, null=False) def _str_(self): return self.name |
2.開啟admin.py撰寫註冊video資料庫到後台管理
1 2 3 4 5 6 | from django.contrib import admin from youtobeapp.models import video # Register your models here. admin.site.register(video) |
2.記得要執行下列指令,建立帳號:demo,密碼:1234
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
3.進入後台
4.建立第一筆資料
5.編輯views.py取得一筆資料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from django.shortcuts import render from youtobeapp.models import video # Create your views here. def home(request): try: v = video.objects.get(name="Django") width=v.width height=v.height src = v.src except: print("沒找到") width=1280 height=720 src = 'W40mDCqWyYo' return render(request, 'index.html', locals()) |
6.執行結果
上一篇文章:影片網站資料庫及後台管理(二) - 視圖和樣版
下一篇文章:影片網站資料庫及後台管理(四) - 讀取所有資料
沒有留言:
張貼留言