【问题标题】:Error while not authenticate users try to see the content未对用户进行身份验证时出错,尝试查看内容
【发布时间】: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


【解决方案1】:

试试这个

def home_screen_view(request, *args, **kwargs):
    all_videos = Video.objects.all()
    V_form = Video_form()
    video_added = False
    
    if not request.user.is_active:
        # any error you want
        return HttpResponse('Inactive User')
    
    try:
        account = Account.objects.get(username=request.user)
    except:
        # any error you want
        return HttpResponse('User does not exits.')
    
    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)

【讨论】:

  • 感谢您回答我的问题,错误消失了,但这不是我想要的,我希望未经身份验证的用户也可以看到视频并感谢您的时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2019-11-19
  • 2020-12-25
  • 2022-07-22
  • 2020-01-06
  • 2018-06-15
  • 2015-12-25
相关资源
最近更新 更多