EdwardRB
EdwardRB

Reputation: 1

NameError: name 'Video' is not defined

I have little problem, I tryed make video uploader but something go wrong models.py

class Video(models.Model):
    name= models.CharField(max_length=500)
    videofile= models.FileField(upload_to='videos/', null=True, verbose_name="")

    def __str__(self):
        return self.name + ": " + str(self.videofile)

Forms.py

from django import forms

class VideoForm(forms.ModelForm):
    class Meta:
        model=Video
        fields=["name", "videofile"]

views.py

from django.shortcuts import render
from .models import Video
from .forms import VideoForm

def showvideo(request):

    lastvideo= Video.objects.last()

    videofile= lastvideo.videofile


    form= VideoForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        form.save()


    context= {'videofile': videofile,
              'form': form
              }


    return render(request, 'Blog/videos.html', context)

And yes I made migrations but he telling me that name 'Video' is not defined

Upvotes: 0

Views: 3274

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

In your forms.py, you specify model = Video, but you forgot to import the Video class, hence in that file, the name Video is not defined.

You can import this like:

# forms.py

from django import forms
from .models import Video

class VideoForm(forms.ModelForm):

    class Meta:
        model = Video
        fields = ["name", "videofile"]

Note that in your view, you should not write form= VideoForm(request.POST or None, request.FILES or None), since then the form becomes invalid if there are simply nor request.POST parameters. You should pass request.POST itself, not request.POST or None.

Upvotes: 2

Related Questions