【问题标题】:How Can I allow user to post a video using this models:如何允许用户使用此模型发布视频:
【发布时间】: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


    【解决方案1】:

    对我来说,解决这个问题最简单的方法是使用CreateView

    views.py文件:

    from django.views.generic import CreateView
    from django.contrib.auth.mixins import LoginRequiredMixin
    
    from .models import Post # Will import Your `Post` model
    
    class PostCreateView(LoginRequiredMixin, CreateView):
        model = Post
        fields = ['title', 'file']
        success_url = '/'
        template_name = 'addvideo.html'
    
        def form_valid(self, form):
            form.instance.user = self.request.user
            return super(PostCreateView, self).form_valid(form)
    

    model 是您的模型的名称(在您的情况下为 Post)。

    fields 是您要在模板中显示的字段列表。

    template_name 是模板的路径。

    success_url是Post创建成功后重定向用户的路径。

    form_valid 会将当前登录的用户保存为帖子的所有者。

    addvideo.html文件:

    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="submit">
    </form>
    

    要在模板上使用自定义样式,您可以使用django-widget-tweaks 包,这里是关于如何使用此包link 的分步教程。

    【讨论】:

    • 在命令提示符下抛出错误
    • 这是错误先生。 URLs.py 第 12 行,在 path('addvideo', views.addvideo, name='addvideo') AttributeError: module 'mokau.views' has no attribute ''addvideo'。
    • mokau 应用程序中签出您的views.py 文件以获得addvideo 视图,如果该文件包含addvideo 视图,那么您可能拼错了视图名称,或者您没有提供正确的视图名称视图的路径。对于我的源代码,路径应如下所示:from &lt;app_name&gt;.models import PostCreateView `urlpatterns = [ path("post/create", PostCreateView.as_view(), name="post_create"),] `
    • 哇,它的工作,非常感谢你
    • 很高兴为您提供帮助! ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2016-10-10
    相关资源
    最近更新 更多