Django Project
Explore how to set up a Django project by creating the necessary directories and files, understanding their purpose, and running the development server. Learn to initiate your first Django application quickly and efficiently within an environment that simplifies dependencies and setup.
We'll cover the following...
Initial set-up for Django
To run a Django application on Educative, we do not need to set anything up. All the code will run inside the special environment within the browser. It’s as easy as that!
Educative is designed in such a way that we do not get distracted by dependencies and instead get right into what is essential: the learning.
Creating a Django project
A Django project is a collection of applications and configurations that, when combined together, will make up the full web application, which is a complete website running with Django. We will discuss what a Django application is in the next lesson.
The following command will create a Django project in the first_project directory:
django-admin startproject first_project
Before running the Django project, let’s look at the files this command created.
These files are:
-
The outer
first_project/root directory, which is a container for our project. Its name doesn’t matter toDjango; we can name it anything we like. -
manage.py: A command-line utility that lets us interact with our Django project in various ways. -
The inner
first_project/directory is the actual Python package for our project. -
first_project/__init__.py: This is a blank Python script that, due to its special name, lets Python know that this directory can be treated as a package. -
first_project/settings.py: This is a Python script where we will store all of our project settings. -
first_project/urls.py: This is a Python script that will store all the URL patterns for our project. Basically, it is where we will store the different pages of our web application. -
first_project/views.py: This is a Python script that will handle all of the requests and send anHttpResponseobject in return. -
first_project/asgi.py: An entry-point for ASGI-compatible web servers to serve our project. -
first_project/wsgi.py: This is a Python script that acts as the Web Server Gateway Interface. It helps us in deploying our web application to production. -
db.sqlite3: This is a database file where all of the generated data will be stored.
Running the project
Now, let’s run our Django project. The following command is used to run the Django project:
python manage.py runserver
For your convenience, we have already run this command. So, you just have to hit the RUN button to run the project.
In the terminal tab
We will see the following output on the command line:
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 06, 2020 - 10:51:26
Django version 2.0.13, using settings 'first_project.settings'
Starting development server at http://0.0.0.0:3000/
Quit the server with CONTROL-C.
In the output tab
We will see a “Congratulations!” page with a rocket taking off. It worked!