Introduction
Welcome to the world of Django, where building web applications becomes a breeze! Whether you're a coding enthusiast or a beginner eager to explore web development, Django is a fantastic framework to kickstart your journey. In this guide, we'll walk through the initial setup and create a simple blog application.
Setting Up Your Environment
1. Install Python
First things first, ensure you have Python installed. Open your terminal and check the Python version:
$ python --version
2. Virtual Environments
Navigate to your project directory and create a virtual environment:
$ cd ~/desktop/code/django
$ python3 -m venv .venv
$ source .venv/bin/activate
To exit the virtual environment later:
(.venv) $ deactivate
3. PyPI and Django
Upgrade pip and install Django:
(.venv) $ python -m pip install --upgrade pip
(.venv) $ python -m pip install django
4. Django Project
Initialize your Django project:
(.venv) $ django-admin startproject django_project .
5. Apply Migrations
Run migrations to set up the initial database:
(.venv) $ python manage.py migrate
6. Run the Development Server
Start the development server:
(.venv) $ python manage.py runserver
Visit http://127.0.0.1:8000/
in your browser to see the Django welcome page.
7. Create a Django App
Now, let's create a Django app for our blog:
(.venv) $ python manage.py startapp django_app
8. Integrate the App
Open django_project/
settings.py
and add your app to the INSTALLED_APPS
:
INSTALLED_APPS = [
# ... other apps ...
"django_app",
]
Building Your First Django Blog
Now that our environment is set up, let's build a simple blog application!
1. Create Models
Define your blog models in django_app/
models.py
. For example:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
2. Make Migrations
Run migrations to apply the model changes:
(.venv) $ python manage.py makemigrations
(.venv) $ python manage.py migrate
3. Admin Panel
Django provides a built-in admin panel. Let's make our models visible there. In django_app/
admin.py
:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
4. Views and Templates
Create views in django_app/
views.py
and templates in the django_app/templates
directory. Don't forget to configure URLs in django_app/
urls.py
.
# django_project/urls.py
from django.contrib import admin
from django.urls import path, include # new
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("pages.urls")), # new
]
5. Run the Server
Start the server again:
(.venv) $ python manage.py runserver
Visit http://127.0.0.1:8000/admin
to log in and manage your posts. Your blog should be accessible at http://127.0.0.1:8000/
.
Conclusion
Congratulations! You've just set up a Django environment and built a basic blog application. This is just the beginning of your Django journey. Stay curious, explore the Django documentation, and keep building! Happy coding!