martin.sh

UNIX is user friendly. It’s just very particular about who its friends are.

check my github
25 March 2024

Setting Up Django: A Step-by-Step Guide

by Martin Chikadaya

In this tutorial, we’ll walk through the process of setting up Django, a popular Python web framework. Let’s get started!

Step 1: Install Django

To begin, make sure you have Python installed on your system. You can download the latest version of Python from the official website. Once you have Python set up, open your terminal and run the following command to install Django:

pip install Django

Step 2: Create a Django Project

After installing Django, navigate to the directory where you want to create your project. Run the following command to create a new Django project:

django-admin startproject myproject

This will create a new directory named myproject with the necessary files and directories for your Django project.

Step 3: Start a Django App

Now, move into the myproject directory by running cd myproject. To start a new Django app, execute the following command:

python manage.py startapp myapp

This will create a directory named myapp inside your project directory, which contains the files for your app.

Step 4: Configure the Database

Django supports various databases, including PostgreSQL, MySQL, and SQLite. Open the settings.py file inside the myproject directory and locate the DATABASES section. Update the configuration according to your preferred database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Step 5: Run Migrations

Django uses migrations to manage database schema changes. To apply the initial migrations, run the following command:

python manage.py migrate

This will create the necessary tables in your database.

Step 6: Run the Development Server

You’re almost done! Start the Django development server by executing the following command:

python manage.py runserver

Open your web browser and visit http://localhost:8000 to see your Django app in action.

That’s it! You’ve successfully set up Django for your project. Happy coding!

Remember to consult the Django documentation for more advanced configurations and features.

Let me know if you have any questions.

tags: Django - Python - Web Frameworks