Now that you know the project's structure, it's time to create it! The project will be dedicated to Alan Smithee, a prolific film director. We'll create the project called smithee, the application movies, one template, and one view handler.
Project structure
Start by running the following code from your shell in any directory you want:
django-admin startproject smithee
cd smithee # we're now in the project dir
python3 manage.py startapp movies
cd movies # and now we're in the application dirAfter creating an application, don't forget to add the application to INSTALLED_APPS in the setting.py file, located in the project directory. In the file, it will look like this:
INSTALLED_APPS = [
# other installed apps
'movies',
]Now let's create a directory for templates. In Django applications, templates are stored in the <app_name>/templates/<app_name> directory. So, for our current app, we'll create the templates directory inside the application movies, and then again the movies subdirectory:
mkdir templates # note that we're in the "smithee/movies" dir
cd templates
mkdir movies # and now we're in the "smithee/movies/templates/movies" dirTemplates and views
It's time to create your first template. To do this, create an index.html and add the following code:
<!DOCTYPE html>
<title>Movies</title>
<h1>Films by {{ director }}</h1>
<ul>
{% for movie in movies %}
<li>{{ movie.year }} - {{ movie.title }}</li>
{% endfor %}
</ul>Don't go deep into the code right now; we'll discuss the Django template language in a separate topic. For now, it's enough for you to note that we print variables in double curly braces, and we also add the for loop to go through the movies list. Where do we get all these variables from? Let's go back to the app directory and jump into the movies/views.py file.
Paste this code to the file:
from django.conf import settings
from django.shortcuts import render
my_movies = [
{
'title': 'Catchfire',
'year': 1990,
},
{
'title': 'Mighty Ducks the Movie: The First Face-Off',
'year': 1997,
},
{
'title': 'Le Zombi de Cap-Rouge',
'year': 1997,
},
]
def all_films(request):
return render(request, 'movies/index.html', {'movies': my_movies, 'director': settings.DIRECTOR})As you can see, we define the my_movies list and add a function to render a page. The render method basically shows the template to users. Again, we'll cover it later, but at this moment, note that it takes a request, our template file index.html, and a dictionary with the required variables. The keys in this dictionary are what we named the corresponding data in the template, and the values are the actual data: the list of dictionaries that will be iterated in the template, and the string with the director name. Note also that for the director, we use the settings.DIRECTOR variable from the settings.py module. Similarly, we can declare some global variables for the whole project. To do this, we need to write the following in the settings.py file:
DIRECTOR = 'Alan Smithee'URL
To make pages visible on specified addresses, we need to define them in the urls.py modules. In Django, we usually have the project-level urls.py module (placed in the project root directory) and then one urls.py for each separate application.
In the project-level urls.py, we will define the URL that will be a base path to the movies app. Let's make it a root. Edit smithee/urls.py as follows:
from django.urls import include, path
urlpatterns = [
# will look for all paths in movies.urls
path('', include('movies.urls')),
]Let's see what this piece of code does. The method include('movies.urls') gathers all the URLs defined in the movies.urls (i.e., the movies/urls.py file) module. The path() method adds the specified prefix (in our case "", an empty string) to all the gathered URLs. This is very useful when we have lots of URLs in every application and don't want to add them all explicitly to the project-level urls.py. The prefix might be any you like. For example, if you have more than one app with similar URLs, you can add different prefixes, for example, app1, app2, and so on, then the URLs for app1 will be routed as /app1/some/url/path.
Also, edit the application-level smithee/movies/urls.py (if this file doesn't exist, just create it):
from django.urls import path
from . import views
urlpatterns = [
path('', views.all_films),
] Here, the path() method connects the " " (root) path to the all_films view from views edited earlier.
Putting all that together, now the index.html template is reachable at the root URL of your application.
Conclusion
Now you know how to create a Django project. You've learned what files you need to prepare, where they can be found, and what part of the project they deal with. We hope that this simple example will provide you with the necessary understanding to build more complex projects in the future.