13.2. Creating a Django Project: Structure of a Django Project

Página 60

Django is a high-level web development framework, written in Python, that promotes rapid development and clean, pragmatic design. It comes with a number of ready-to-use tools and functionality that can help developers create robust and efficient web applications. This course chapter will guide you through the process of creating a new Django project and explore the structure of a Django project.

Creating a new Django project

To create a new Django project, you must first have Django installed on your system. If you already have Django installed, you can create a new Django project using the following command:

django-admin startproject project_name

This command will create a new directory with the name you gave your project. This directory is the root of your Django project and contains all the files needed for your project.

Structure of a Django project

A Django project is made up of a set of settings for an instance of Django, including database configuration, Django-specific options, and application-specific settings. Let's take a look at the default structure of a Django project.

project name/
    manage.py
    project name/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

Here is a brief description of each of the files and directories:

  • manage.py: This is a command line utility that lets you interact with your Django project in a variety of ways. For example, you can use this file to create tables in your database or start a web server for your project.
  • project_name/__init__.py: This is an empty file that tells Python that this directory should be considered a Python package.
  • project_name/settings.py: This file contains all the settings for your Django project. You will reference this file whenever you need to change something related to your project's configuration.
  • project_name/urls.py: This file is used to define URL patterns for your project. In other words, this file tells Django which pages to display for which URLs.
  • project_name/asgi.py: This file is used to serve your project when it is deployed to an ASGI server. ASGI is the asynchronous server gateway specification that allows Django to handle asynchronous traffic.
  • project_name/wsgi.py: This file is used to serve your project when it is deployed to a WSGI server. WSGI is the web server gateway specification that allows Django to handle synchronous traffic.

This is the basic structure of a Django project. As you start adding applications to your project, you'll see more files and directories being added to this structure. However, the basic structure will remain the same.

In summary, creating a Django project involves creating a workbench, setting up a database, defining URLs, and creating views and models. The Django project structure is designed to be flexible and easy to expand, making it an excellent choice for projects of all sizes.

We hope this chapter has given you a clear understanding of creating a Django project and the structure of a Django project. In the next chapter, we'll dive deeper into Django application development.

Now answer the exercise about the content:

What is the role of the "settings.py" file in a Django project?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

6113.3. Creating a Django Project: Database Setup

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text