【发布时间】:2021-02-20 07:40:13
【问题描述】:
不存在于 / 帐户匹配查询不存在。
未验证用户尝试观看视频时出现错误 我需要让未经身份验证的用户可以看到内容视频,但绝对不能发布任何视频
型号
class Video(models.Model):
author = models.ForeignKey(Account, on_delete=models.CASCADE)
video = models.FileField(upload_to='post-videos')
title = models.CharField(max_length=100)
description = models.TextField(null=True, blank=True)
allow_comments = models.BooleanField(default=False)
is_public = models.BooleanField(default=False)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
publish_date = models.DateTimeField(null=True, blank=True)
表格
class Video_form(forms.ModelForm):
class Meta:
model = Video
fields = ('title', 'description', 'video')
观点
def home_screen_view(request, *args, **kwargs):
all_videos = Video.objects.all()
V_form = Video_form()
video_added = False
account = Account.objects.get(username=request.user)
if 'submit_v_form' in request.POST:
print(request.POST)
V_form = Video_form(request.POST, request.FILES)
if V_form.is_valid():
instance = V_form.save(commit=False)
instance.author = account
instance.save()
V_form = Video_form()
video_added = True
contex = {
'all_videos': all_videos,
'account': account,
'V_form': V_form,
'video_added': video_added
}
return render(request, "personal/home.html", contex)
HTML 模板
{% if request.user.is_authenticated %}
<div class="container">
<div class="mt-5">
<form action="." method="post" enctype="multipart/form-data">
{% csrf_token %}
{{V_form}}
<button class="btn btn-success btn-lg" name="submit_v_form">Upload</button>
</form>
</div>
{% endif %}
<hr>
{% for x in all_videos %}
<h3 class="text-center mt-2 mb-2">{{x.title}}</h3>
<video class="embed-responsive embed-responsive-16by9" controls="controls" >
<source src="{{x.video.url}}" type="video/mp4" />
</video>
{% endfor %}
【问题讨论】:
-
我认为views.py中的错误
标签: django django-models django-views django-forms django-templates