Django is a powerful web framework for Python that allows you to build web applications quickly and efficiently. This guide will walk you through the process of setting up a Django project from scratch, perfect for beginners who want to get started with web development using Django.
Table of Contents
- Installing Python
- Setting Up a Virtual Environment
- Installing Django
- Creating a Django Project
- Creating a Django App
- Configuring the Database
- Creating Models
- Creating Views
- Creating Templates
- Configuring URLs
- Running the Development Server
1. Installing Python
Before we start with Django, make sure you have Python installed on your system. Different Django version requires different Python versions.
To check if Python is installed, open your terminal or command prompt and type:
python --version
If Python is not installed, download and install it from the official Python website (https://www.python.org/downloads/).
2. Setting Up a Virtual Environment
It's a good practice to create a virtual environment for each Django project. This keeps your project dependencies isolated from other projects.
To create a virtual environment:
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your project.
- Run the following command:
python -m venv myenv
This creates a new virtual environment named "myenv".
To activate the virtual environment:
- On Windows:
myenv\Scripts\activate
- On macOS and Linux:
source myenv/bin/activate
You should see your prompt change to indicate that the virtual environment is active.
3. Installing Django
With your virtual environment activated, install Django using pip:
pip install django
4. Creating a Django Project
Now that Django is installed, let's create a new project:
django-admin startproject myproject
This creates a new directory called "myproject" with the basic Django project structure.
Navigate into the project directory:
cd myproject
5. Creating a Django App
Django projects are made up of one or more apps. Let's create an app for our project:
python manage.py startapp myapp
This creates a new directory called "myapp" with the basic app structure.
Open the myproject/settings.py
file and add your new app to the INSTALLED_APPS
list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # Add this line
]
6. Configuring the Database
Django uses SQLite as its default database, which is fine for development. The database configuration is already set up in myproject/settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
To create the database tables, run:
python manage.py migrate
7. Creating Models
Models define the structure of your database. Let's create a simple model in myapp/models.py
:
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __str__(self):
return self.name
After creating or modifying models, run these commands to create and apply migrations:
python manage.py makemigrations
python manage.py migrate
8. Creating Views
Views handle the logic of your application. Create a simple view in myapp/views.py
:
from django.shortcuts import render
from .models import Item
def item_list(request):
items = Item.objects.all()
return render(request, 'myapp/item_list.html', {'items': items})
9. Creating Templates
Templates are HTML files that define how your data is displayed. Create a new directory myapp/templates/myapp/
and add a file item_list.html
:
<!DOCTYPE html>
<html>
<head>
<title>Item List</title>
</head>
<body>
<h1>Items</h1>
<ul>
{% for item in items %}
<li>{{ item.name }} - {{ item.description }}</li>
{% endfor %}
</ul>
</body>
</html>
10. Configuring URLs
To make your view accessible, you need to configure URLs. First, in myproject/urls.py
:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
Then, create a new file myapp/urls.py
:
from django.urls import path
from . import views
urlpatterns = [
path('', views.item_list, name='item_list'),
]
11. Running the Development Server
You're now ready to run your Django project! Use this command:
python manage.py runserver
Visit http://127.0.0.1:8000/
in your web browser to see your Django application in action.
Congratulations! You've set up a basic Django project. This foundation will allow you to expand your project by adding more models, views, and templates as needed.
Follow me on my social media platforms for more updates and insights:
- Twitter: @rupeshmisra2002
- LinkedIn: Rupesh Mishra
- GitHub: Rupesh Mishra
Remember to always activate your virtual environment before working on your project, and to run migrations whenever you make changes to your models.
Top comments (0)