DEV Community

Dickson
Dickson

Posted on • Updated on

Creating a Django application

Django is a high-level Python web framework that simplifies the process of web development. It abstracts away some common repetitive tasks such as user authentication, content administration and site maps, allowing developers to focus on application-specific logic. Django also addresses the well-known security concerns, and ensures good performance even under heavy loads.

The benefits brought by Django make it a popular choice for web development across various domains, ranging from web applications and portals, to APIs and microservices. This article will guide through the creation of a basic Django project and application.

Prerequisites

  • Python 3
  • pip

Setting up the environment

  1. Create a new virtual environment named djangoenv.

    python -m venv ~/.virtualenvs/djangoenv

    Note: It is a good idea to keep all the virtual environments in a single folder. In this guide, it is in .virtualenvs/ in the home directory.

  2. Activate the virtual environment.

    Unix: source ~/.virtualenvs/djangoenv/bin/activate
    Windows: %HOMEPATH%\.virtualenvs\djangoenv\Scripts\activate.bat

    Note: To deactivate the virtual environment, simply run deactivate. Remember to reactivate the environment whenever you need to install new packages or start the server.

  3. Install Django.

    python -m pip install Django

Creating a Django project

django-admin startproject djangoproj

This command generates a Django project named djangoproj with the following directory structure.

djangoproj/
    djangoproj/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
    db.sqlite3
    manage.py
Enter fullscreen mode Exit fullscreen mode

Starting the development server

  1. Change into the outer djangoproj directory.

    cd djangoproj

  2. Run the Django site locally.

    python manage.py runserver

    The console should show an output similar to the following. To stop the development server, press Ctrl+C.

    Watching for file changes with StatReloader
    Performing system checks...
    
    System check identified no issues (0 silenced).
    
    You have 18 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.
    
    March 5, 2024 - 00:00:00
    Django version 5.0.3, using settings 'djangoproj.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.
    

Creating a Django application

Note: A project is a collection of configurations and apps for a particular website. An application is responsible for delivering a service, such as a blog system or a database of public records. A project can contain multiple applications; an application can be in multiple projects.

The project is now set up, and it is ready to create new applications by running python manage.py startapp <app_name>.

Follow the tutorial attached in the references to create and customize the views and models that build up the application. Alternatively, check out and install some third-party applications, such as the Django REST framework for building web APIs.

References

Top comments (0)