In this tutorial, we will learn how to create a new Django project from scratch. Django is a high-level Python web framework that promotes rapid development and clean, pragmatic design.
We will cover how to:
Prerequisites:
pip install Django
in your terminal.A Django project is a collection of settings and configurations for an instance of Django, including database configuration, Django-specific options, and application-specific settings.
To create a new Django project, navigate to the directory where you want your project to live, and run the following command:
django-admin startproject my_project
Replace my_project
with your preferred project name. Django will create a new directory with the same name as your project, which will contain the basic files and directories necessary for a Django project.
After running the above command, your project directory should look like this:
my_project/
manage.py
my_project/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Here's what each file does:
manage.py
: A command-line utility that lets you interact with your Django project.my_project/
: The inner my_project/
directory is the actual Python package for your project.__init__.py
: An empty file that tells Python that this directory should be considered a Python package.settings.py
: Settings/configuration for this Django project.urls.py
: The URL declarations for this Django project.asgi.py
: An entry-point for ASGI-compatible web servers.wsgi.py
: An entry-point for WSGI-compatible web servers.To verify that your project has been set up correctly, navigate to your project directory (my_project/
) and run the following command:
python manage.py runserver
This command starts the Django development server, which is a lightweight web server written purely in Python. After running the command, you should see output similar to the following:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
You can now open your web browser and visit http://127.0.0.1:8000/ to view your new Django project.
In this tutorial, we learned how to create a new Django project, explored the structure and purpose of various files in a Django project, and ran our project using Django's development server.
For further learning, you can explore how to create and manage Django applications, how to connect Django to a database, and how to use Django's models and views to design your website's backend.
settings.py
file in your new Django project to change the timezone to your local timezone. (Hint: Look for the TIME_ZONE
setting.)Solutions:
settings.py
and find the TIME_ZONE
setting. Change its value to your local timezone, for example, 'America/New_York'.python manage.py runserver
. Open your web browser and visit http://127.0.0.1:8000/.Keep practicing and exploring Django to become more comfortable with it. Happy coding!