Post
model would represent blog posts.Django’s ORM allows you to interact with the database using Python code instead of writing raw SQL queries. It provides a high - level, Pythonic way to create, read, update, and delete database records.
Django comes with a built - in admin interface that allows you to manage your application’s data easily. You can create, edit, and delete records without writing any additional code.
First, make sure you have Python installed on your system. Then, install Django using pip
:
pip install django
Next, create a new Django project:
django - admin startproject myproject
cd myproject
To run the development server:
python manage.py runserver
You should see a “Congratulations!” page in your browser at http://127.0.0.1:8000/
.
In Django, an app is a self - contained module that performs a specific task. To create a new app, run the following command in your project directory:
python manage.py startapp myapp
You need to add your app to the INSTALLED_APPS
list in myproject/settings.py
:
# myproject/settings.py
INSTALLED_APPS = [
#...
'myapp',
]
Models are defined in the models.py
file of your app. Let’s create a simple Article
model:
# myapp/models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.title
After defining the model, create the database tables by running the following commands:
python manage.py makemigrations
python manage.py migrate
Views are defined in the views.py
file of your app. Let’s create a simple view to display a list of articles:
# myapp/views.py
from django.shortcuts import render
from .models import Article
def article_list(request):
articles = Article.objects.all()
return render(request, 'myapp/article_list.html', {'articles': articles})
Create a templates
directory inside your app directory, and then create a myapp
directory inside the templates
directory. Inside myapp
, create an article_list.html
file:
<!-- myapp/templates/myapp/article_list.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Article List</title>
</head>
<body>
<h1>Articles</h1>
{% for article in articles %}
<h2>{{ article.title }}</h2>
<p>{{ article.content }}</p>
<p>Published on: {{ article.pub_date }}</p>
{% endfor %}
</body>
</html>
First, create a urls.py
file in your app directory:
# myapp/urls.py
from django.urls import path
from .views import article_list
urlpatterns = [
path('', article_list, name='article_list'),
]
Then, include your app’s URLs in the project’s urls.py
:
# myproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('myapp.urls')),
]
select_related
and prefetch_related
to reduce the number of queries.Building your first web app with Django can be a rewarding experience. Django’s high - level features, such as the ORM, admin interface, and MVT pattern, make it easy to develop complex web applications quickly. By understanding the core concepts, following best practices, and avoiding common pitfalls, you can build robust and scalable web applications.