DEV Community

Cover image for Setup and Create your First Django Application
Manasa SK
Manasa SK

Posted on • Updated on

Setup and Create your First Django Application

Django is a python based high-level web framework which is primarily known for rapid development. It supports the Model View Controller (MVC) pattern.

You can observe multiple websites that are built on this framework such as:

If you're new to web development, Django is a fairly easy framework to understand and work on. This was my first web framework and I would suggest it if:

  • Your project involves Machine Learning models or is of Scientific nature

  • You have some experience or knowledge of Python


Pre-requisites

While Django framework is much simpler to setup relative to other technologies, some fundamental pre requisites include:

  • Installation of Python If python is not installed, click here and install one of the newer versions of python.

  • Basics of Python Concept of OOPs, loops and conditional statements, data types and variables, functions and basic data structures are sufficient to get started.


Setup Django

Create a new folder at your desired location and open the same in the terminal

PS C:\Users\Manasa\Desktop\first_project>
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment to install Django

PS C:\Users\Manasa\Desktop\first_project> python -m venv env
Enter fullscreen mode Exit fullscreen mode

Here, env is the name of your virtual environment.

A virtual environment is created for you to install a custom version of Python and its different packages which is not connected to the global installation on the server.

Once the environment is created, activate it by running the command

PS C:\Users\Manasa\Desktop\first_project> env/scripts/activate
Enter fullscreen mode Exit fullscreen mode

Activated environment should look like this:

(env) PS C:\Users\Manasa\Desktop\first_project>
Enter fullscreen mode Exit fullscreen mode

Now, install Django

 pip install django
Enter fullscreen mode Exit fullscreen mode

You can check the installation by calling django-admin
It should give this output:

(env) PS C:\Users\Manasa\Desktop\first_project> django-admin

Type 'django-admin help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    optimizemigration
    runserver
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
(env) PS C:\Users\Manasa\Desktop\first_project>
Enter fullscreen mode Exit fullscreen mode

Create Project

Now that Django is successfully installed, you can create your project and applications.

Run the following command

 django-admin startproject first_project .
Enter fullscreen mode Exit fullscreen mode

Here, your project is named 'first_project'.

NOTE: '.' at the end of the command is essential as the project structure by default makes the file manage.py inaccessible to a number of files. This can cause numerous errors going ahead.

Now, create your app by running the command

 django-admin startapp myapp
Enter fullscreen mode Exit fullscreen mode

Here, your application is named 'myapp'.

Now that your project and application are created, use manage.py to make further changes.

Its functionality can be observed by the following command.

(env) PS C:\Users\Manasa\Desktop\first_project> python manage.py

Type 'manage.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[contenttypes]
    remove_stale_contenttypes

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    optimizemigration
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver
(env) PS C:\Users\Manasa\Desktop\first_project>
Enter fullscreen mode Exit fullscreen mode

If this command throws an error, check whether you added '.' while creating the project.

The next step is optional but recommended. Migrate the initial changes by running the command

 python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

This command sets up the default database that Django provides.

Finally, run the server locally. The following command should give the result.

(env) PS C:\Users\Manasa\Desktop\first_project> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 07, 2022 - 16:21:53
Django version 4.1, using settings 'first_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Enter fullscreen mode Exit fullscreen mode

http://127.0.0.1:8000/ is the link to the locally hosted application

If you see this page on following the link:

Django Project Hosted

Your project is successfully set up and ready to run!

Top comments (0)