Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It simplifies building robust web applications by providing reusable components and following the "Don't repeat yourself" (DRY) principle.
This tutorial will guide you through the process of creating a simple Django project. By the end, you'll have a working web application with basic functionality, including models, views, templates, and a simple database integration.
Before starting with Django, make sure you have Python installed on your machine.
A virtual environment helps isolate project dependencies. You can create a virtual environment using the following steps:
Install virtualenv
:
pip install virtualenv
Create a project directory:
mkdir myproject
cd myproject
Create a virtual environment:
virtualenv venv
Activate the virtual environment:
venv\Scripts\activate
source venv/bin/activate
Once your virtual environment is set up, you can install Django with the following command:
pip install django
To verify that Django was installed correctly, run:
django-admin --version
This should display the installed version of Django.
A Django project consists of settings, URLs, and other configurations. To create a project:
Create a new Django project:
django-admin startproject mysite
Navigate into your project directory:
cd mysite
Run the development server: To verify that everything is set up, run the following command:
python manage.py runserver
You should see a message indicating that the server is running. Open a browser and navigate to http://127.0.0.1:8000/
. You should see the default Django welcome page.
In Django, a project can contain multiple apps. An app is a web component that provides specific functionality to your site.
Create a new app: To create an app called blog
, run the following command:
python manage.py startapp blog
This will create a directory named blog
with several files, including models.py
, views.py
, and urls.py
.
Add the app to your project: Open mysite/settings.py
and add 'blog'
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',
'blog', # Add this line
]
In Django, models are used to define the structure of your database. Let’s create a simple blog model with a title and content.
Open blog/models.py
and add the following code:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Create the database table: Run the following command to create the table for your new Post
model:
python manage.py makemigrations
python manage.py migrate
Django will generate SQL statements and apply them to the database.
Django comes with an admin interface that allows you to manage your data. To access it, you need to create a superuser.
Create a superuser: Run the following command:
python manage.py createsuperuser
You will be prompted to enter a username, email address, and password.
Run the server:
python manage.py runserver
Visit http://127.0.0.1:8000/admin/
in your browser and log in with the superuser credentials. You can now manage your Post
model through the admin interface.
Views in Django are used to display content to the user. Let’s create a view to display the list of blog posts.
Open blog/views.py
and add the following code:
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts})
This view fetches all Post
objects from the database and passes them to the post_list.html
template.
Templates in Django are HTML files used to render views. Let's create a template to display the blog posts.
Create a templates
folder inside the blog
directory.
Inside the templates
folder, create a blog
folder. Inside the blog
folder, create a file called post_list.html
:
<!DOCTYPE html>
<html>
<head>
<title>Blog Posts</title>
</head>
<body>
<h1>Blog Posts</h1>
<ul>
{% for post in posts %}
<li>
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<small>Created at: {{ post.created_at }}</small>
</li>
{% endfor %}
</ul>
</body>
</html>
In Django, URLs are used to map views to specific paths in the web browser.
Open blog/urls.py
and add the following code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.post_list, name='post_list'),
]
Connect the app’s URLs to the project: Open mysite/urls.py
and include the blog app’s URLs.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')), # Add this line
]
Run the development server:
python manage.py runserver
Now, visit http://127.0.0.1:8000/
in your browser. You should see a list of blog posts. If you haven’t created any posts yet, you can create them through the Django admin interface at http://127.0.0.1:8000/admin/
.
In this tutorial, you’ve created a simple Django web application that displays blog posts. You learned about the following concepts:
From here, you can expand the project by adding features like post creation, editing, and deletion, or exploring Django’s authentication system to allow user accounts.
You can find the official Django documentation at Django Docs for more in-depth explanations and advanced topics.
Sign up to our newsletter to receive our latest news and products. Stay updated on the latest developments and special
CyberCodings is online web development tutorials platform for everyone. Learn java, python, django,JavaScript, React.js, Next.js and Node.js, Mongodb, Fullstack find inspiration for HTML, CSS,JQuery and Bootstraps and many more web technologies and web design-development .
Become a web developer with cybercodings.com