基于django的视频网站搭建

 

步骤一.settings配置

 

万事开头难,对于django来说,只要我们把settings里面的配置配置清楚,其实后面的程序也就很简单了,首先我们找到app配置项,添加我们需要配置的app,这里面app,即submit就是我们第一个注册的app,其作用是上传视频,以及相应的用户信息

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'submit'
]

 找到templates配置项,并如下配置

'DIRS': [
            BASE_DIR / 'templates'
        ],

 找到数据库,切换成mysql的配置

DATABASES = {
    'default': {
    # 连接本地mysql数据库
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'video',#你的数据库名
        'USER': 'root',# 你的用户名
        'PASSWORD':'root',#你的密码
        'HOST': '127.0.0.1',# 本地连接
        'PORT': '3306',# 本地端口号
    }
}

 配置静态static文件,和媒体存放的文件(日后会放到阿里云oss上面,现在视频文件就暂时储存在项目src里面的media文件夹里面

STATIC_URL = 'static/'
STATICFILES_DIRS = [
    BASE_DIR / 'static'
]


MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

 

步骤二.基础的templates,models,view,urls

这里来到了这个功能比较复杂的地方,其实说复杂也不复杂,其主要流程就是通过view层获得通过form层处理后,获得前端数据,最后依旧是在view层,储存数据,并保存到mysql ,首先先创建最基本的urls,view,是的localhost:8000得以访问

# app的urls层
from django.urls import path
from . import views

urlpatterns = [
    path('',view=views.submit,name='submit')
]
# 项目的urls
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('submit.urls'))
]

然后是view层

from django.shortcuts import render
def submit(request):
    return render(request,'submit.html')

最后是templates层

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    hhh
</body>
</html>

项目一旦启动,逻辑是通过项目自己的路由找到submit.urls,然后submit.urls里面找到submit的views视图函数,进入submit的视图函数之后渲染一个名为“submit.html”的前端界面,如果settings,这些配置都是完善的,成功打开了submit.html就前进了一大步,之后就是正式进入我们的需求部分了

步骤三,进阶配置

了解需求我们是需要一个模板来接收前端的字段,并且视频文件,这里卡了有点久,一方面是想一来就直接上传到oss,但是不是很熟练,所以搞了几天都还没有搞出来,所以就先决定放在media目录下面,首先是models层,我们需要最终是要做一个视频教育网站,包括教案,课件,视频文件,类型,封面,视屏简介,所以我们配置models如下

from django.db import models
from django.core.validators import FileExtensionValidator
# Create your models here.
class video(models.Model):
    #id默认编号
    Video_id = models.AutoField(primary_key = True)
    #标题
    Video_title = models.CharField(max_length=30)
    #标签
    Video_tag = models.CharField(max_length=30)
    #内容简介
    Video_content = models.TextField(max_length=300)
    #课件
    Video_source = models.FileField(upload_to='video_presentation/%Y/%m/%d/',validators=[FileExtensionValidator(['ppt'])])
    #教案
    Video_lesson_plan = models.FileField(upload_to='video_lesson_plan/%Y/%m/%d/',validators=[FileExtensionValidator(['docx'])])
    #视频
    Video_file = models.FileField(upload_to='video_file/%Y/%m/%d/',validators=[FileExtensionValidator(['jpg,png,gif,bmp,tiff'])])
    #封面
    Video_cover_img = models.FileField(upload_to='video_cover_img/%Y/%m/%d/',validators=[FileExtensionValidator(['mp4,avi,mov,wmv,mkv'])])
    
    #拥有者
    Video_owner = models.CharField(max_length=40,default="SteveR")
    #发布时间
    video_pubtime = models.TimeField(auto_now=False, auto_now_add=True)

    def __str__(self):
        return self.Video_id

 大家如果需要上传视频的话只需要关注video_file字段即可,upload_to是media下面的路径,validator是文件的要求类型,大家models搞好了之后不要忘记在src目录下执行,正常执行之后django的数据就会迁移到mysql里面,前提是settings里面配置好了mysql,且电脑里面又安装了mysql,以及环境变量,

python manage.py makemigrations
python manage.py migrate

mysql里面会是这样的

接下来是urls不需要改动,接下来是view层

from django.shortcuts import render
from .forms import LessonPlanForm
from .models import video
from django.db import connection
# Create your views here.
def submit(request):
    if request.method == 'POST':
        form = LessonPlanForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                # 处理表单数据
                newVideo = video()

                newVideo.Video_title = form.cleaned_data['title']
                newVideo.Video_tag = form.cleaned_data['tags']
                newVideo.Video_content = form.cleaned_data['content']
                newVideo.Video_source = form.cleaned_data['presentation']
                newVideo.Video_lesson_plan = form.cleaned_data['lesson_plan']
                newVideo.Video_cover_img = form.cleaned_data['cover_image']
                newVideo.Video_file = form.cleaned_data['video']
                print(newVideo.Video_id,newVideo.Video_title,newVideo.Video_content,newVideo.Video_file)
                # 保存数据、文件等操作
                newVideo.save()
                #
                return render(request, 'submit.html')
            except Exception as e:
                print(str(e))
                print(connection.queries)
    else:
        print("2222")
        form = LessonPlanForm()
    
    return render(request, 'submit.html', {'form': form})

form层

from django import forms

class LessonPlanForm(forms.Form):
    title = forms.CharField(label="标题",required=True)
    tags = forms.CharField(label="标签",required=True)
    content = forms.CharField(label="内容", widget=forms.Textarea,required=True)

    presentation = forms.FileField(label="上传课件",required=True)
    lesson_plan = forms.FileField(label="上传教案",required=True)

    cover_image = forms.ImageField(label="上传封面",required=True)
    video = forms.FileField(label="上传视频",required=True)

个人理解数据库<models层<view层<form层<前端,models层数据定义好之后,先迁移至mysql数据库,然后再view层生成一个实例对象,然后这个实例对象的每一个值是通过form与前端比对的

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值