Hello, welcome to the first part of this tutorial-course in which you'll learn and understand the basics of Django by building a clone of the popular website Netflix.
This tutorial assumes that you have at least basics knowledge of:
- Python3 programming language
- Linux
- Html and CSS
- Mysql.
Presentation of Django
Django is one of the most popular Frameworks to build full-stack websites and also APIs. It follows the MVT (Model, views, and template) architecture.
What are Models: The models are python object representations of our database. For example, the class User represents the user's table in our database and an instance of that User class is a record in the table users. Then, each attribute of the user class is a column of the table users* and the value of each attribute for a specific instance of User class, is the value of the column related to that attribute. Don't worry if it sounds weird now, you'll understand better in deep before the end of this tutorial when we will play with models.
What are Templates: To make it simple, templates are what end-users see (the interface made by HTML and CSS, button, images, text, animations, etc...).
What are Views: Views are the listeners to users' actions on the templates. User action might trigger a request to a View. The view will perform an operation related to handling user requests, if required it will interact with the database (throughout models) and return a response to the user if needed. The response could be another template or html page, or a Json response or even another type of response.
In addition, an important thing to know for the beginning, is that a Django project is divided into small reusable pieces (python modules) called apps.
Django installation
To install Django on your system, I advise you to:
work on a virtual environnemt or a conda workspace. But first, make sure python is installed on your computer.
Create a
requirements.txt
file that will contain the list of python libraries and their versions required by our project to work well on any computer. For now, we just needDjango
so ourrequirements.txt
will just contain one line like this:
Django==4.0.2
At the time of this article, the latest stable version of Django is 4.0.2
.
- Then run
pip install -r requirements.txt
orpython -m pip install requirements.txt
(or it could also bepython3
instead of justpython
orpip3
instead ofpip
). That will install all the packages listed in the file with the specified versions.
Create the Django project
Let's use django_netflix_clone for the project name. (I used underscore because it improves the readability.
- Then run:
django-admin startproject django_netflix_clone
.
Note: django-admin
is the Django command-line utility. Itβs used for common operations in the Django development phase as we will see in the following.
A new folder named django_netflix_clone
will be created.
I advise you to move the requirements.txt
file into django_netflix_clone
folder, so it will be inside the project.
- Run:
mv requirements.txt django_netflix_clone
.
The django_netflix_clone
project architecture should looks like this now:
You'll notice that Django has created some files and a default folder with the same name as the project django_netflix_clone
.
At the root of the project we should have the following:
manage.py file: it is a Python file helper used to execute commands related to the project. We will see its uses as we go.
django_netflix_clone folder. We said that a Django project is made of apps. That folder is the default
app
automatically created by Django. It contains some auto-generated files as well. Let's explain a bit them:wsgi.py (Web Server Gateway Interface): When you want to deploy your Django to production in a real server, this file is used as the entrypoint, to allow the application server to connect to your Django project.
asgi.py (Asynchronous Server Gateway Interface): To make it simple, it plays the same role as wsgi.py, but is for asynchronous web servers and applications. So it's used in the place of wsgi.py when your project supports asynchronous tasks.
-
settings.py: It's the project configuration file. All the apps and projects settings are defined on it.
The important settings here are:-
SECRET_KEY
: it's is used to provide a cryptographic signing to the project. It is mostly used to sign session cookies (we will talk about sessions and cookies later in this course). If one were to have this key, they would be able to modify the cookies sent by the application. -
INSTALLED_APPS
: it's an array that contains the list of apps in the project. There are default apps added by Django likedjango.contrib.admin
to manage administration (we will cover it later in this tutorial),django.contrib.auth
to manage authentication, etc... (The apps names a self-explanatory) -
MIDDLEWARE
: Middlewares are functions that are executed before each request. Defaults middlewares are added by django likedjango.middleware.security.SecurityMiddleware
to verify the security of request.
-
We will explain others settings as we go.
-
urls.py: This file describes all the paths that could be accessed in the project. By default it contains one path (
admin
):
urlpatterns = [
path('admin/', admin.site.urls),
]
At this step, we can say that the allowed URLs on our app are http://my_project_url
and http://my_project_url/admin/
Then enter the project:
cd django_netflix_clone
- Open the project in a code editor (Me I use VSCode or visual studio code)
code .
Create our working app
The default app is meant to be a centralizes app. Let's create another app named netflix
to handle our Netflix
features.
- On VS code open a new terminal by clicking
Terminal -> New terminal
from the top horizontal bar menu. It should open a terminal at the root project directory. - Then type this command:
django-admin startapp netflix
. A new folder namednetflix
will be created:
- open the
settings.py
file and addnetflix
app intoINSTALLED_APPS
setting as described above: This will register ournetflix
app to the project. Without that, the app is created but not linked to the project.
We won't explain all the files and folders that are newly created under netflix
app folder now. Let's just explain views.py
and models.py
:
- views.py: it is the file that will contain our views described at the top. Each view is a class, and we will cover them in deep later in the course.
- models.py: It's the file that contains our models. As described above, in Django our database is represented by Django classes.
Design our database
We have created our project and added our main app. Now let's design our database according to our features. Remember, each model is a python class and inherits from the internal Django class django.db.models.Model
.
Open the
models.py
file that is insidenetflix
directory.We have users that will register and watch movies. By default, Django have what we call
contribs
which are pre-made modules to ease our work. From those contribs we have a defaultUser
model. You can customize the default DjangoUser
model as described here. But, in this tutorial, we will just use the default one.
Movie models
- We will have movies to watch. Let's create a model class to represent a movie.
class Movie(models.Model):
"""Movie model class."""
That means, we will have a table named Movie
in our database.
Each movie will have a name, let's add it:
CHARS_MAX_LENGTH: int = 150
class Movie(models.Model):
"""Movie model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
The name column should be a string that why we used the Django model field named CharField
. We've decided to limit the name length to 150 chars.
With the line CHARS_MAX_LENGTH: int = 150
we define a constant to avoid using magic numbers in our code that we will use.
With line name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
. we ask Django to add a column named name
into our movies
table and each record of movie should not have a name with more than 150 chars. blank=True
means that, when we want to create or modify a record of the movie, we don't need to provide the name value. You'll understand more in the following.
- Each movie could have a description, let's add it with this line:
description = models.TextField(blank=True, null=True)
For it we use TextField
instead of CharField
as the description length is not predictable, but we know movies names are not that long.
Note null=True
means that we can create a Movie without specifying a value for the description. If null=True
was not specified like for name
, trying to create a movie without providing description
will raise an exception.
We might need to keep the creation date of the movie, let do it:
date_created = models.DateTimeField(auto_now_add=True)
.
Not that we used DateTimeField
because it's a date and we set attribute auto_now_add
to True
so the creation date will automatically be set by Django during object creation.
Our models.py
should looks like this now:
from django.utils import timezone
from django.db import models
class Movie(models.Model):
"""Movie model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
description = models.TextField(blank=True, null=True)
date_created = models.DateTimeField(default=timezone.now)
- Each movie should have only one category. It's better to store categories in the database as well so we can manage them. Let's create a Category model before the Movie model with
name
anddescription
(the category creation date is not really useful):
class Category(models.Model):
"""Category model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
description = models.TextField(blank=True, null=True)
- We will suppose for this tutorial that a movie must belong to one and only one category. That defines what we call a OneToMany relationship. In Django it's represented by
django.db.models.ForeignKey
. Let's add it:category = models.ForeignKey( Category, models.CASCADE )
The attribute models.CASCADE
mean that if the category is deleted, all its movies should be deleted to avoid data invalid values (For example having some movie with non-existant category).
Your code should looks like this now:
class Movie(models.Model):
"""Movie model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
description = models.TextField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE) # <----------- Add this line
date_created = models.DateTimeField(auto_now_add=True)
- Each movie will have one or more tags. It's better to store tags as well in the database. Let's create a Tag model before the Movie model with
name
anddescription
(the category creation date is not really useful):
class Tag(models.Model):
"""Tag model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
description = models.TextField(blank=True, null=True)
- We will suppose for this tutorial that a movie could have many tags and a tag could be associated to many categories. So that defines what we call a
ManyToMany
relationship. In django it's represented bydjango.db.models.ManyToManyField
. Let's add it:tags = models.ManyToManyField(Tag)
. Your code should then looks like this:
class Movie(models.Model):
"""Movie model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
description = models.TextField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag) # <--- Add this line
date_created = models.DateTimeField(auto_now_add=True)
Note: The attribute models.CASCADE
mean that if a tag is deleted, all it's related movies will also be deleted to avoid data invalid values (For example having some movies with non-existant tag).
We might need some statistics about each movie like the number of people who watched it. Let's add it:
watch_count = models.IntegerField(default=0)
.
Here, as it is a number, we usedIntegerField
anddefault = 0
means that the default value is0
. And plus, as we defined a default value, even if we don't provide that attribute, django won't raise an exception, but set the attribute to0
.Naturally, we need to store the movie video file. For that
Nice there we go. We have all the minimal information for our database.
Managing static files
Before that, we will explain how Django manages files. By django serve all files related to the website (html, css, js, images) to the value of STATIC_URL
parameter in settings.py
file. And the default value is /static/
.
We also need to define the static root folder which is also static/
:
STATIC_URL = 'static/'
STATIC_ROOT = 'static/' # Add this line
You can change it with another value, but I recommend you to keep it only if you have a good reason to change it.
During development, Django recommends storing those files are stored in a folder called static
inside each app.
Managing media files
Now the files that are uploaded into the projects (like our movies videos) are served to the value of MEDIA_URL
parameter in settings.py
. That setting is not there by default, but it recommended value is /media/
so we have to add it (we could add it right after STATIC_URL
):
MEDIA_URL = 'media/'
By default, media files are not stored in the database but in a folder defined by the settings MEDIA_ROOT
. That setting also was not there so we have to define it.
import os # Add this at the top of the settings.py file
...
...
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') # Add this line too
.
That means, our movies will be uploaded into a folder named media
at the root of the directory. We used os.path.join
and BASE_DIR
(defined at the top of the file) to get the full path of the folder in the system. That folder didn't exists, so let's create it:
Make sure to be at the root of the project and run:
mkdir media
.
- The last part to finalize the setup of static and media files is to add their values to the path. Open
urls.py
file like this:
from django.contrib import admin
from django.urls import path
from django.conf import settings # Add this line
from django.conf.urls.static import static # Add this line
urlpatterns = [
path('admin/', admin.site.urls),
]
# Add the lines below
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
We have finished to set up static and media files for our project.
We can then use django.db.models.FileField
to store the link to our movie (media) like this:
file = models.FileField(upload_to='movies/')
.
- A last thing, we need a preview picture for our movie. As it's a picture, Let use
django.db.models.ImageField
like this:
preview_image = models.ImageField(upload_to='preview_images/')
.
Our final Movie
class file looks like this:
class Movie(models.Model):
"""Movie model class."""
name = models.CharField(max_length=CHARS_MAX_LENGTH, blank=True)
description = models.TextField(blank=True, null=True)
categories = models.ForeignKey(Category, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag)
watch_count = models.IntegerField(default=0)
file = models.FileField(upload_to='movies/')
preview_image = models.ImageField(upload_to='preview_images/')
date_created = models.DateTimeField(default=timezone.now)
There we go
Django Migrations
We have finished to set up our basic database. Now we need to create what we call migrations
. Migrations are Python files containing the definition and history of changes in your models. To create migration we should run the command:
python manage.py makemigrations
. Note that we have used the project command tool manage.py
.
After running the command, we should get this error:
It's because it's a module required by ImageField
to work that is not yet installed in our environment. So we can add it to our requirements.txt
file like this:
django==4.0.2
Pillow==9.0.1 # Add this line into requirements.txt
.
Then run pip install -r requirements.txt
again (it will only install missing modules, in our case pillow
).
Then run again: python manage.py makemigrations
.
You should have this result:
.
That will generate some files (migrations) into migrations
folder under netflix
module. Those files contain descriptions of our models that will be used to physically create and/update our database.
Migrate database
To physically create our database, we need to specify the database type in the settings. If you open the settings.py
file, you'll see:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
'ENGINE': 'django.db.backends.sqlite3'
means our database is SQLite
and 'NAME': BASE_DIR / 'db.sqlite3',
is named db.sqlite3
and should be at the root of the project. Django support multiple others backend like MysQL
that we will see in Part5 of this course. For the time being we will stick to the defaults settings.
Now that we see that our database was already set up but understood which setting handle it, let's populate our db.sqlite3
database with our models throughout migrations. Run python manage.py migrate
. You'll see a message like this:
The model Manager
Each Django model has something called Manager and comes with a default one named objects
. It allows us to perform queries to the database from our model. For example, basic queries we can do on the Movie
model are:
-
Create a movie: it's achieved with
Movie.objects.create(name="Avatar")
-
Get all movies: it's achieved with
Movie.objects.all()
. - *Filter all movies whose name starts with uppercase
A
*: it's achieved withMovie.objects.filter(name__startswith="A")
. -
Filter all movies whose name starts with
a
no matter the case (A
ora
): it's achieved withMovie.objects.filter(name__istartswith="a")
. If it is still a bit fuzzy for you, it's okay, it will become clear after you learn how to play with the manager in the right next section.
Django Shell: play with our data
To quickly test our models and the manager, Django provides a very useful tool called Django Shell.
- Open a terminal on VSCode (by default it will be opened ad the root of the project).
Run:
python manage.py shell
. A new command prompt will be shown.
Import our models: type
from netflix.models import Category, Movie, Tag
and press enter.
- Let's try to create a movie named
Dracula3
: run
Movie.objects.create(name="Dracula")
You should have an error similar to this:
sqlite3.IntegrityError: NOT NULL constraint failed: netflix_movie.category_id
And it is normal, because, we cannot create a Movie without a Category. Remember that a movie should belong to only one category because of the relationship category = models.ForeignKey(Category, ...
.
So we need first to create a category. Let's create a category named tech
. Run the following command:
tech_category = Category.objects.create(name="Tech")
The new created category is then assigned to the shell
variable tech_category
.
We can verify it informations.
- In the terminal type just
tech_category
and press enter and it should display this:
>>> tech_category
<Category: Category object (1)>
Note that Category object (1)
is the default name used by python to render an object instance. We can customize the name by overriding the method __str__
of our models class.
For the Category
model we can return the category's name instead like this:
class Category(models.Model):
...
# Add the function below
def __str__(self):
return self.name
We can return also do the name for Category
and Tag
models.
class Movie(models.Model):
...
# Add this function
def __str__(self):
return self.name
class Tag(models.Model):
...
# Add this function
def __str__(self):
return self.name
Now, close django shell by executing
exit()
command and reopen it withpython manage.py shell
.Let's import our models again
>>> from netflix.models import Category, Tag, Movie
>>>
- Let retrieve our category. To do so we have several ways:
- you can run
tech_category = Category.objects.get(name="Tech")
Here we made a direct access. The particularity here is that if there is was category with name Tech
, it will throws an excpetion. To verify run
Category.objects.get(name="NonExistentName")
You'll get this:
>>> Category.objects.get(name="NonExistentName")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/horacefayomi/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/horacefayomi/.local/lib/python3.10/site-packages/django/db/models/query.py", line 650, in get
raise self.model.DoesNotExist(
netflix.models.Category.DoesNotExist: Category matching query does not exist.
- you can also access the object by it primary key, which is even better than using any other keys
tech_category = Category.objects.get(pk=1)
- you can access the category using `filter` method that we saw in previous point:
tech_category = Category.objects.filter(name="Tech").first()
Note that Category.objects.filter(name="Tech")
return an array of the the matching categories.
You can verify by running:
>>> Category.objects.filter(name="Tech")
<QuerySet [<Category: Tech>]>
Then chaining with .first()
return the first element of the list.
Now you would have notice that now the category is display like
<Category: Tech>
instead of
<Category: Category object (1)>
That means our __str__
methods works well ^_^.
We then created our first category instance.
- In the terminal, if you type
tech_category.pk
and press enter you will see:
>>> tech_category.pk
1
That means the value of the primary key of our moel is 1
.
It's because, as we have seen before, in Django the default database is sqlite, and in sqlite like most of others relational databases, the primary key is by default an integer auto-generated, starting from 1.
- We can also check the category name in the terminal by typing
tech_category.name
. Here should be the output:
>>> tech_category.name
'Tech'
- Now we should be able to finally create our first movie:
movie = Movie.objects.create(name="Dracula", category=tech_category)
Now we can verify the attributes of our movie
>>> movie = Movie.objects.create(name="Dracula", category=tech_category)
>>> movie.pk
1
>>> movie.name
'Dracula'
>>> movie.category
<Category: Tech>
>>> movie.date_created
datetime.datetime(2022, 11, 7, 16, 54, 11, 259483, tzinfo=datetime.timezone.utc)
Awesome, our first movie is also well created.
- We can list all the current movies of our database to see:
>>> Movie.objects.all()
<QuerySet [<Movie: Dracula>]>
- We can also create a movie in another way. Execute
new_movie = Movie(name="Harry potter3")
- set the movie category
new_movie.category = tech_category
At this step the movie object is created, but the movie is not yet save into the database. To save it we should call the save()
method of the model instance
new_movie.save()
And there we go. You can lost again all the movies to see that the new movie is among the list:
>>> Movie.objects.all()
<QuerySet [<Movie: Dracula>, <Movie: Harry potter3>]>
Nice.
- You can check that the
watch_count
of thenew_movie
movie is0
.
>>> new_movie.watch_count
0
- We can update the count to any number.
>>> new_movie.watch_count = 10
>>> new_movie.save()
>>>
Now running new_movie.watch_count
should display 10
>>> new_movie.watch_count
10
- To see the number of movies in the database you can run
Movie.objects.count()
and it should print 2:
>>> Movie.objects.count()
2
- You can also remove a movie by calling it
delete()
. Let's removenew_movie
:
>>> new_movie.delete()
(1, {'netflix.Movie': 1})
- We can check that the count is now
1
>>> Movie.objects.count()
1
I hope you enjoyed palying with the models in Django shell.
We just learn the basics operations that you'll use to perform on your models. We will see more in the coming parts of this course.
Django ORM
Django models are class abstractions that represents our database, Even if we perform operations using either the default model manager (Movie.objects.create()
) or on the model instance new_movie.save()
, all those operations are translated in background into SQL queries by Django ORM (Object-Relational Mapper). Django's ORM is just a pythonical way to create SQL to queries, manipulate our database and get results in a pythonic fashion. Awesome right !!!
Register Admin
Now that you know how to manipulate your data on the shell, let's see how to manipulate it through a graphical interface.
Django allows us to easily administrate our database with a powerful tool called Django Admin. Basically, it provided an administration interfaces where we can easily list, create, modify, view, and delete items of our database. And to set up the admin features is easy like eating a cake.
Reminder, it's the contrib 'django.contrib.admin'
that allows provide us that powerful feature.
Open the admin.py
file under netflix
folder. And add those lines:
from netflix.models import Movie
from netflix.models import Category
from netflix.models import Tag
admin.site.register(Movie)
admin.site.register(Category)
admin.site.register(Tag)
That's all.
Note: The admin interface is customizable but we won't cover this in this tutorial.
Create a superuser
To manage our admin site we need to create a super user. It's a user with enough privilege to access the administration interface. By default, a created user is not a superuser. We will dive deep into Django authentication and roles in the next parts of this tutorial course.
To create a superuer run in a porject temrinal in VScode:
-
python manage.py createsuperuser
. It will ask to enter some values. Let use admin for username, admin@admin.com for the email, and type admin, for the password. It will print some warnings:
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create a user anyway? [y/N]:
Press Y
and continue, we will come back later on that in the next parts of the tutorial.
There we Go.
Start development server and add some data
Django comes with an internal light development server. To start it, open a new terminal in the project inside VScode as usual and run:
python manage.py runserver
. You'll see this message:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 27, 2022 - 02:28:29
Django version 4.0.2, using settings 'django_netflix_clone.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
That means, the server has started and is available at host http://127.0.0.1:8000/
. You can specify your own port with python manage.py runserver 0.0.0.0:<your_port>
.
Login to the Admin interface and play with data
- Open your favorite browser (me I use chrome).
- Type the development URL, you should see this page
- Open the admin page: got to
http://127.0.0.1:8000/admin/
on the browser. It looks like this:
-It's the login page. Enter the credentials for the super user (username = "admin", password = "admin") and validate the form.
- You'll be redirected to the admin dashboard presented below:
We will come back later on User
and Group
. Let's play with our Media
, Category
, and Tag
. Click on Category
and add some categories. The interface is intuitive. You have the Add
button at the top right corner.
- Add Tags with the name
Trending
,Popular
. Set the description you want.
- Add categories named
Action
,Adventure
,Romance
,Thriller
.
- Add Spider-Man: No Way Home movie:
- Add
Spider-Man: No Way Home
for the name and description - Select the category
- Select tags (To select multiple tags as a movie could have more that one tag, keep this pressed down and select the tags with right mouse clicks)
- Download the image from the internet to your computer and add it to the
preview-image
field.
- Add
Currently, the movie page looks like this:
We can a bit customize Django admin to allow super users to see the preview_image
and description
of a movie.
- Open
admin.py
file - Remove line
admin.site.register(Movie)
And add these code:
from django.utils.html import format_html # Add this line
...
...
# add the lines below
class MovieAdmin(admin.ModelAdmin):
def preview(self, movie):
"""Render preview image as html image."""
return format_html(
f'<img style="height: 200px" src="/media/{movie.preview_image}" />'
)
def video(self, movie):
"""Render movie video as html image."""
return format_html(
f"""
<video width="320" height="240" controls>
<source src="/media/{movie.file}" type="video/mp4">
Your browser does not support the video tag.
</video>"""
)
preview.short_description = 'Movie image'
video.short_description = 'Movie video'
list_display = ['name', 'preview', 'video', 'description']
admin.site.register(Movie, MovieAdmin)
- We used the Django class
django.contrib.admin.ModelAdmin
to customize how our Movie will be displayed. We used the django formatterdjango.utils.html.format_html
to render thepreview_image
andfile
video as HTML. - With
list_display = ['name', 'preview', 'video', 'description']
we ask django to display the computedpreview
andvideo
and also thename
anddescription
attributes of eachMovie
record of the database.
And this is the result:
NB: The full source code is available here: https://github.com/fayomihorace/django-netflix-clone
Congratulations.
We are at the end of this first part.
Summary
In this tutorial you've learned:
- what is Django
- how to create a Django project
- how to create an app and link it to the project
- the basics Django files architectures
- the basics Django settings
- what is a model and how to create models
- How to use the basic Django model fields
- what is Django ORM
- what is a Django Model manager and how to perform basic database operations on them
- How to use Django Shell to easily play with our models
- what are migrations, and how to create them
- How to start a Django development server
- what are statics files and media files and how to set up them
- How to register your models with Django admin and create a superuser
- How to manage our database using the Django admin interface.
If you faced any blocker or error while following this tutorial, please drop a comment, I'll reply to help you as soon as possible. Excited to have you in part 2 of this tutorial-course.
Top comments (14)
When you do the python command manage.py makemigrations for the Django migrations topic, you get two categories:
But also in Media, you have two sub-files that appear movies and preview_images. I don't get those categories either. Can you also explain to me where my problem might be?
Hey Yvan, it's not normal indeed. Are you sure you've added the models code into your
models.py
file.yes, I added the models code in my models.py file but it didn't work. And I can't go further when I try to replicate your model for the database. Could you help me please ?
Yes, you could either:
1- push the current state of your code on github and send me the link so I'll pull and debug for you
Or
2- we could schedule a live pair programming debugging session
Which one works best for you ?
Anyways, here is my github email: fayomihorace@gmail.com
Ans if you go for the pair programming, write me to this email folahanfayomi@gmail.com.
For the two sub-files in media folder don't worry, it's not required at this step of the tutorial even though it is in the final source code.
Thank you
Hello everybody, I'm a bit stack here.
Django Shell: play with our data
To quickly test our models and the manager, Django provides a very useful tool called Django Shell.
Open a terminal on VSCode (by default it will be opened ad the root of the project).
----------C:\shared\netflix_django\django_netflix_clone>
---------- I can open a terminal on VSCode but when I Run -------
Run: python man shell. A new command prompt will be shown.
---------- I've got the error:
C:\shared\netflix_django\django_netflix_clone>python man shell
python: can't open file 'C:\shared\netflix_django\django_netflix_clone\man': [Errno 2] No such file or directory
Also, I don't understand this line........
Import our Movie models: from netflix.models import Movie
any help, explain, π©π©
Also the line:
from netflix.models import Movie
is a module importation. It's more a python stuff here. We have a class calledMovie
inside the filemodels
.Remember that to be able to use a function or class that is not available in a given file, you should import that function or class from it source inside the file that should use it.
Let take a simple exemple, guess you have this architechure:
And
file1.py
contains this:and then you want to use the util_function of the UtilClass inside
file2.py
,you can import them like this:
I fyou got that, you should also know that, as we need to import a class or function or module in order to use it in another file, we should import them into django shell too. That why we need to import
Movie
model if we want to test it into django shell. And becasue the django shell root folder is the project root folderdjango_netflix_clone
, which contains all our app folders modules (netflix
included), and also becauseMovie
is inside the pathdjango_netflix_clone/netflix/models.py
to import it we dofrom netflix/models import Movie
.Hope that help you.
Hello Taller. That might happen because you're on windows instead of linux.
Let me test it on windows and give you a feedback soon.
Hello all,
I setup the virtual environment so I've got the folder estructure. I'm using VScode.
I set up the virtual environment on my xampp server.
everything was working, folders and files structure.
I stopped before to do the migrations.
but when I disconnected the ftp from the server and open the project again some of the files had disappeared.
there is only two folders and two files:
django_netflix_clone
django_netflix_clone/
settings.py
netflix/
models.py
requirements.txt
can anybody tell me why?
thanks
Are you sure you saved the files before you deconnected from FTP ?
Do you have
auto save
enabled on VScode ?Hello Orace,
Well, yes, I do have auto save enabled on my VScode, and yes I always check if everything has been saved on my server via FTP.
Any other ideas?
Thx
Hum, I used to work with git, never worked directly with a remote server.
Currently I have no cluefor you.
But I recommends you to not work directly on the server, work locally on git, and progressivlely commit your changes to git and pull the working branch on your sever.
Hello,
What is the way to put this app on a server.
Let's say....
Www.clone.xx
Which is my local server
Thanks