【发布时间】:2022-01-23 09:40:00
【问题描述】:
我希望经过身份验证的用户能够在此应用程序中发布我该怎么做? 我在 django 的 sqlite admin 中对其进行了测试,它工作正常,现在我想允许用户从 addvideo 模板发布:
这是模型:
from django.db import models
from django.db.models.base import Model
from django.db.models.fields import CharField
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(null=False, blank=False)
title = CharField(max_length=25, blank=False)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
这是我的 addvideo 模板:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="card">
<div class="form-group m-3">
<label>Upload Your Video</label><br><br>
<input required
name="video"
type="file"
accept="video/*"
class="form-control-file">
</div>
<div class="form-group m-3">
<label for="title">Your Topic</label>
<input type="text" class="form-control" name="title" id="title">
</div>
<button type="submit" class="btn btn-primary">Add Post</button>
</div>
</form>
</div>
</div>
</div>
这是我的views.py文件:
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from .models import Post
def addvideo(request):
posting = Post.objects.all()
if request.method == 'POST':
file = request.FILES.get('video')
posting = Post.objects.create(
file=file
)
return redirect('home')
return render(request, 'addvideo.html', {'posting': posting})
def dashboard(request):
posting = Post.objects.select_related('user')
return render(request, 'dashboard.html', {'posting': posting})
def home(request):
posting = Post.objects.select_related('user')
return render(request, 'home.html', {'posting': posting})
def viewVideo(request, pk):
posting = Post.objects.get(id=pk)
return render(request, 'video.html', {'posting': posting })
【问题讨论】:
标签: django-models django-views django-templates