DEV Community

Tommi k Vincent
Tommi k Vincent

Posted on

Getting Started with Django: A Step-by-Step Guide

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development,In this guide I will help on how to setup Django from scratch.

1 Setting up a django project
first check if python is installed on your computer running

python3 --version
Enter fullscreen mode Exit fullscreen mode

on your terminal,if not installed follow the above link to install python on your computer.https://www.python.org/

Create a directory where you will store your work for easy efficiency ,on your terminal run

mkidr Learning-django
Enter fullscreen mode Exit fullscreen mode

then

cd Learning-django
Enter fullscreen mode Exit fullscreen mode

to change into your working directory.
Setting up a Virtual Environment
t's a good practice to create a virtual environment for each Django project to keep dependencies up and running the python code consistently.

# Create a virtual environment
python -m venv  env

# Activate the virtual environment
# For Windows:
env\Scripts\activate

# For macOS/Linux:
source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Once activated, your terminal prompt should reflect the virtual environment above.

Installing Django
Now that your virtual enviroment is set up install as below.
django py running

pip install django
Enter fullscreen mode Exit fullscreen mode

verify the installation with

django-admin --version
Enter fullscreen mode Exit fullscreen mode

step2 creating new djangoproject
with Django installed, you create a new Django project by running

django-admin startproject mysite
cd mysite
Enter fullscreen mode Exit fullscreen mode

This will create the following structure

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
Enter fullscreen mode Exit fullscreen mode
  • manage.py: A command-line utility that lets you interact with this Django project in various ways.

  • mysite/: A directory that is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).

  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package.

  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

  • mysite/urls.py:URL declarations for routing requests.

  • mysite/asgi.py:An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.

  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

step3 Running the development server

To see if everything is set up correctly, run the development server :

python3 manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Visit http://127.0.0.1:8000/ in your browser, and you should see the Django welcome page.
Django comes with a default SQLite database with well be using for this article.

db.sqlite3  manage.py  mysite
Enter fullscreen mode Exit fullscreen mode

Top comments (0)