Creating A New Django Project
Creating A New Django Project
We can start our project by using a Django command, called django-admin, and then a sub-command,
startproject, then, the name of our project, smartnotes, and then dot to indicate we want to create the
project in this folder.
so this command creates two things: a managed.py file and a folder called smartnotes, the same name
we gave our project.
The managed.py file will be the entry point of your project. With it, you'll be able to run your project
server, manually work with the database, and much more. If you look inside the smartnotes
folder, you'll notice that all of the files here are related to the configuration of your project. The two
main files you'll use here are the urls.py file, where you're going to configure, well, the URLs of your
project, and the settings.py.
Let's take a quick look into the settings.py. You can see here that there is a lot of global variables being
defined. For instance, the debug equals true here tells Django that we are working in a development
environment. Because we're in this safe development environment, it can return much more
information when something goes wrong than it would in a production environment. You don't really
have to understand everything it has right now, we'll get to it eventually, the only thing you need to
know is that this is where you have the base of your project.
You can use Python, then the file, manage.py, and the command runserver to create a server using
the default configurations we have right here.
We can see here that there is some warnings in red, but you don't have to worry about it for now. You
can also see that we're using Django Version 3.2, and that the configuration file being used is the
smartnotes.settings.
This means that this server is using this settings.py inside the smartnotes folder as the basis of it. You
can also see that it gives a link to a web page hosted in the port 8000 of your local host. This means that
your project is up and running on your computer and you can access it by using your browser. Let's
click here and see what happens. You can see here that it will open a web page with the default
layout. This means that you have successfully created and ran your first Django project.
You can see now that we have two folders: smartnotes, which is our settings folder, and home, which
is the Django app we just created.
Every time we create an app, we need to add it to the settings file so that it knows that that folder is part
of the project we're running. Let's open the settings.py file. And add the name of our app in the
INSTALLED_APPS variable. In order to make things a bit more organized, I always leave a small
comment separating this app, created by us, from those that were already installed by default.
Now that we have our project started, our app configured, it's time to create our first view.
Let's go to the home, then go to the views file, and write our first function.
views.py of home app As you can see, this is a pre-configured file. So we need to create our functions
here. and now we create def home.
Let's import from django.http import HttpResponse.
It receives a request. And it return an HttpResponse with a simple message. Hello, world! As you can
see, this function is saying that every time it receives a request, it will return a response with the text
Hello, world! , but how does it know when to send a request to this function? Well, that's why we have
the urls.py file. We can go back now to the smartnotes, urls.py, and import this file there so we can
have access to this function. So from home, let's import views, and now here inside the
urlpatterns, we're going to add a new path. Let's call it home.And let's say views.home as
When a person goes to the home endpoint, they're making a request to that path. Django will go to the
urls.py file to see if it's ready to receive a request at this path. Since it is, it will go to the views
file, finally arriving to the function we defined. Since the function received the request, it can then
respond with a message Hello, world!
Django uses a common pattern as the way of structuring its project called Model View Template
framework, or MVT. Views are responsible for handling requests and responses. Views can be as
simple as functions, and can respond with something as simple as pure text. There are yet two
additional layers for us to get familiarized with, right? These additional layers will allow us to increase
our project's complexity, while being simple tools to work with. The model layer handles the data
and how it's stored. The template layer allow us to render the information coming from the
database into lovely HTML pages.
Creating first Django template:
How to return HTML pages by using the Django template language. Let's start by creating a folder
called templates inside our app folder. And inside of this folder, we'll also create another folder with
the same name as our app, so let's call it home. This might seem weird, but it will allow us to quickly
identify where a template is located, even if we don't know in which app we are on. This is a typical
organization pattern for Django templates. Inside this folder, let's create a file called
welcome.html. And inside it, we're going to add a basic HTML page. So a html tag, let's add a header
with a title, so SmartNotes, and then a body with just one Welcome to SmartNotes. Okay, so now we
can go back to the views file and change our base function. Instead of using the HttpResponse, we'll
use the function render from the Django shortcuts that's already imported here. To use this function, we
need to pass three parameters, the original request, the name of the template, and empty brackets. I
know there is a warning here in the terminal that sounds a bit scary, but don't worry, this is normal, and
we'll get to it later. Let's save this file and check the home end point again. And voila, our beautiful
HTML is now being rendered. You might be wondering why we left the empty brackets behind,
right? Although we are writing an HTML page, Django is actually using a template framework to create
the final HTML page that we see in the browser. We can use the brackets at the end of the function as
a way of passing down information from the view to the template. So let's import the datetime
model and pass today's date into the dictionary with a key called today, datetime.today. We can then
modify our template to receive a variable called today by defining it between double brackets. Today is
today. Let's save it and try it again. There it is. This is why we're not using pure HTML, but actually a
backend framework for defining templates called Django template language, or DTL. The HTML file
we wrote will pass through the detail mechanism and it will insert the variables passed on the
dictionary of the render function down to the template. This looks simple now, but DTL allows us to
use sophisticated programming logic for creating dynamic HTML pages with very little effort.
Selecting transcript lines in this section will navigate to timestamp in the video
- [Instructor] Earlier, we talked why apps are a good way to organize our project. Let's
take a minute to understand a bit more why this type of modularization is such an
important concept. As you've seen, an app is a small library that is contained inside
your Django project. We can have as many apps as we want, however, as the project
grows, if we don't take care of it, things can start to get messy and things will fly
between the apps. That's why we need to understand what we need to do to make
good Django apps. Good software projects should be well modularize, and your
Django project should be no different. That's why each app should be self-
contained, which means that everything you need for that app should live inside
it. That's why we created the folder template inside it. The ideal app is one where you
can delete the folder and do nothing else, the Django project just continued to work
perfectly. So far, we're almost there, but there's still one thing that we need to do to
clean things up. When we created our first endpoint, we had to import the fuse file
from home into the URLs file in the smart notes folder. This creates a dependency
that wouldn't allow us to quickly delete the home app. Let's make things a bit more
organized. Okay, so first thing is to create another URL file inside the home app. In
here, we're going to create a file, very similar to the one we have on the smart notes
app. And let's add the same endpoint that we had in the previous file, views.home
using the home function. Okay, so now comes the fun part, in the smart notes, let's
get the URLs file. We can delete the dependencies that we implemented here, and
now from the jungle URLs, we're also going to import the include function. Now,
what we need to do is add an import path, but instead of using a function, we're
going to use the include function here to pass that file as a string. So path, let's leave
it open for now, and then let's include the home.urls file. Let's save this, and if we go
back to browser, yes, as you can see here, nothing changed except the fact that
now, if we delete the whole app, these won't give any errors because the app is not
being important on this smart notes URLs file. There, with just a couple of tweaks, we
eliminated a dependency and your project is following good standards of Django
applications.