Home / Blogs

How to Set Up a Django Project in 10 Steps [Complete Guide]

Django
·

June 16, 2023

how-to-set-up-a-django-project-in-10-steps

Are you eager to start your Django web development journey but feeling overwhelmed by the project setup process? Fear not! In this blog post, we’ll walk you through a quick and easy guide to set up your Django project. With just a few steps, you’ll have your development environment up and running smoothly. Let’s dive right in!

Step 1: Install Python and pip:

First things first, ensure that Python is installed on your system. Visit the Python website (https://www.python.org) and download the latest version compatible with your operating system. Once installed, verify that Python is added to your system’s PATH. Additionally, pip, the Python package installer, should also be installed automatically with Python.

Step 2: Create a Virtual Environment:

Using virtual environments is a best practice to keep your project dependencies isolated. Open your terminal and navigate to the desired directory for your project. To create a virtual environment, execute the following command:

python -m venv myenv

Here, myenv is the name of your virtual environment, which you can customize.

Step 3: Activate the Virtual Environment:

To activate the virtual environment, run the appropriate command for your operating system:

For Windows:

myenv\Scripts\activate

For macOS and Linux:

source myenv/bin/activate

Once activated, you should see the name of your virtual environment displayed in the terminal prompt.

Step 4: Install Django:

With your virtual environment active, it’s time to install Django. Execute the following command:

pip install Django

This will download and install the latest stable version of Django.

Step 5: Create a Django Project:

Navigate to the desired directory where you want to create your Django project, and then execute the following command to create a new project:

django-admin startproject myproject

Here, myproject is the name of your project. Feel free to choose a name that suits your project’s purpose.

Step 6: Run the Development Server:

Move into the project directory by executing:

cd myproject

To ensure everything is set up correctly, run the development server using the following command:

python manage.py runserver

Step 7: Start an App:

Django follows the “app” concept to organize functionality within a project. To create a new app, navigate to the project directory in your terminal and execute the following command:

python manage.py startapp myapp

Step 8: Configure the App:

After creating the app, you need to configure it within your Django project. Open the settings.py file located in your project directory and find the INSTALLED_APPS list. This list contains the names of all the apps installed in your project.

Add your app to the list by including ‘myapp’ (replace ‘myapp’ with the actual name of your app) as a string. It should look like this:

INSTALLED_APPS = [
    ...
    'myapp',
    ...
]

By adding your app to the INSTALLED_APPS, Django will recognize and include it in your project’s functionality. This step ensures that Django knows to look for your app’s models, views, and other components when running the project.

Now, your app is properly configured within the Django project, and you can proceed to define models, views, and other features specific to your app.

Remember to save the settings.py file after making any changes.

Step 9: Define Models and Views:

With your app set up, you can start defining models and views to build your application’s functionality. Open the models.py file within your app’s directory and define your database models using Django’s Model syntax. Next, create views in the views.py file to handle HTTP requests and render templates.

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.name

Step 10: Run Migrations:

Migrations are a crucial part of Django’s database schema evolution. To apply your model changes to the database, run the following command in your terminal:

python manage.py makemigrations

This command generates migration files based on the changes you made to your models. To apply those migrations, execute:

python manage.py migrate

By default, the server will run on http://127.0.0.1:8000/. Open your browser and visit this URL to see the default Django welcome page. Your Django project is now up and running. You’ve successfully completed the setup process and can start developing your web application. Now you can leverage the power of Django’s features, including its ORM, URL routing, templating, and more, to build your project according to your requirements. Enjoy the journey of Django web development and bring your ideas to life!

Conclusion:

Setting up a Django project doesn’t have to be a daunting task. By following these simple steps, you can quickly create a Django project and get started with web development. Remember to use virtual environments for a clean and isolated development environment. Now that you have your project set up, you’re ready to unleash the power of Django and bring your web ideas to life.

Horilla Editorial Team Author

Horilla Editorial Team is a group of experienced writers and editors who are passionate about HR software. We have a deep understanding of the HR landscape and are committed to providing our readers with the most up-to-date and informative content. We have written extensively on a variety of HR software topics, including applicant tracking systems, performance management software, and payroll software etc. We are always looking for new ways to share our knowledge with the HR community. If you have a question about HR software, please don't hesitate to contact us.