seeb4coding python

How to Set Up Your First Django Project

Django is a popular web framework written in Python that makes it easy to build powerful web applications quickly. This step-by-step guide will help you set up your development environment and create your first Django project. Let’s dive right into it!

Step 1: Install Python

Ensure that you have Python installed on your system. If you haven’t installed it yet, head over to the official Python website and download the latest version.

Step 2: Check Python Version

After installing, verify the installation and check the version of Python using this command in your terminal:

python --version

If Python is successfully installed, it will display the version number, e.g., Python 3.8.5.

Step 3: Create a Virtual Environment

Using a virtual environment is a good practice to manage dependencies for different projects. Here’s how you can create a virtual environment named django-venv:

python -m venv django-venv

After running this command, a folder named django-venv will be created in your project directory.

Step 4: Navigate to the Virtual Environment Folder

Change your current directory to the newly created virtual environment:

cd django-venv

Step 5: Activate the Virtual Environment

To start using your virtual environment, activate it by navigating to the Scripts folder and using the activate command:

For Windows:

cd Scripts
activate

For macOS/Linux:

source bin/activate

After activation, your terminal will display the virtual environment name in parentheses, like this:

(django-venv) c:\Users\YourName\Desktop\django-venv\Scripts>

To deactivate the virtual environment later, just use the command:

deactivate

Step 6: Install Django in the Virtual Environment

Now that your virtual environment is activated, you should install Django within it. Use this command:

(django-venv) python -m pip install django

Step 7: Verify Django Installation

To ensure that Django was installed successfully, check the Django version with:

(django-venv) python -m django --version

You should see the installed Django version, such as 3.2.7.

Creating a Django Project

Once the virtual environment is set up and Django is installed, let’s create a new Django project.

Step 8: Create a New Django Project

Navigate to the location where you want to create your project and run:

(django-venv) django-admin startproject myfirstproject

This command will create a new Django project named myfirstproject. The project structure should look like this:

myfirstproject/
├── manage.py
└── myfirstproject/
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Step 9: Navigate to Your Project Folder

Change your directory to the newly created project folder:

cd myfirstproject

Step 10: Run the Development Server

Now that your Django project is set up, let’s run the development server to check if everything is working:

(django-venv) python manage.py runserver

Open your browser and go to http://127.0.0.1:8000/. You should see the Django welcome page!

Additional Steps: Adding HTML, CSS, and JavaScript Files

To add static files (HTML, CSS, and JavaScript) to your Django project:

Step 11: Create a Templates Folder

Create a folder named templates in your project directory:

myfirstproject/
└── templates/

Step 12: Create an index.html File

Inside the templates folder, create an index.html file and add your HTML content:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Welcome to Django</title>
  <style>
    body {
      background-color: #f4f4f4;
      font-family: Arial, sans-serif;
      text-align: center;
      padding-top: 50px;
    }
  </style>
</head>
<body>
  <h1>Welcome to Your First Django Project</h1>
</body>
</html>

Step 13: Update views.py

In the myfirstproject folder, update the views.py file to render the index.html file. If views.py does not exist, create it:

from django.shortcuts import render

def home(request):
    return render(request, 'index.html')

Step 14: Update urls.py

In the urls.py file, map the URL to the new view:

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
]

Step 15: Restart Your Server

Restart your server to apply the changes:

(django-venv) python manage.py runserver

Now, navigate to http://127.0.0.1:8000/, and you should see your custom HTML page!

Conclusion

Congratulations! You’ve successfully set up a Django project, created a virtual environment, and configured a basic HTML file to display on your website. Keep experimenting with Django to explore its powerful features.

Happy coding! ✨

Leave a Reply

Your email address will not be published. Required fields are marked *