Django Vidly Project

I am undertaking the python course, and currently working on the vidly project alongside Mosh. I have done everything that he has showed me, and I can’t see my static files at the front page. [Refer to the error below]

Someone kindly help me fix this.

UnboundLocalError at /movies/

local variable ‘movies’ referenced before assignment

Request Method: GET
Request URL: http://127.0.0.1:8000/movies/
Django Version: 2.1
Exception Type: UnboundLocalError
Exception Value: local variable ‘movies’ referenced before assignment
Exception Location: C:\Users\user\Desktop\vidly\movies\views.py in index, line 7
Python Executable: C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.4
Python Path: [‘C:\Users\user\Desktop\vidly’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\python39.zip’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\DLLs’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\lib’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages’]
Server time: Fri, 7 May 2021 08:12:57 +0000

ANOTHER ERROR IN ADMIN WHEN I TRY TO CREATE A GENRE/MOVIE

OperationalError at /admin/movies/genre/add/

no such table: main.auth_user__old

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/movies/genre/add/
Django Version: 2.1
Exception Type: OperationalError
Exception Value: no such table: main.auth_user__old
Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 296
Python Executable: C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.4
Python Path: [‘C:\Users\user\Desktop\vidly’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\python39.zip’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\DLLs’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\lib’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39’, ‘C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages’]
Server time: Fri, 7 May 2021 08:38:52 +0000

local variable ‘movies’ referenced before assignment

Fix this

How can I do that? Or should I just go back and start again?

Sounds like you didn’t declare variable movies.

Error is in Views.py Line 7

I want you to share views.py screenshot

from django.http import HttpResponse

from django.shortcuts import render

from .models import movies

Create your views here.

def index(request):

movies = movies.objects.all()

output= ','.join([m.title for m in movies])

return HttpResponse(output)

Sorry I still unable to find error

I want you to share screenshot of models.py and app → url.py file also

urls_in_main_folder
urls
app
views
models

Have you migrate those models?
Have your app registered in settings ?

Correction :

Name of movie model
And remove first return statement in index function

Make it simple first

def index(request):
object = ModelName.objects.all()
return HttpResponse(object)

I got the second error too. Have you figure it out?

I can add new genre in SQLite DB Browser but can’t add anything on the website created

An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn’t have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can’t modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a global variable inside a function, you must use the global keyword.