Getting Started with Django: A Beginner's Guide
Introduction
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It was designed to help developers take applications from concept to completion as quickly as possible. If you're new to Django or web development in general, this guide will help you get started by covering the essential concepts, tools, and code snippets needed to build your first Django project.
Why Choose Django?
Django is one of the most popular web frameworks, and for good reasons:
This tutorial gives an overview of how to get started with Django to build your first web application. If you want a more robust and in-depth tutorial, use this detailed guide.
Setting Up Your Environment
Before you can start building with Django, you need to set up your development environment.
Step 1: Install Python
Django is a Python framework, so the first thing you need is Python. Django works with Python 3.6 and later. You can download the latest version of Python from the official website. You can use these guides to install Python and VS Code on Macbooks and Windows.
Verify the installation by opening your terminal or command prompt and typing:
python --version or python3 --version
Step 2: Install Django
Once Python is installed, you can install Django using pip, the Python package manager. It's a good practice to create a virtual environment for your project to manage dependencies. Use this guide to learn to create and activate Python virtual environments in your operating system.
Install Django: pip install django
Verify the installation: python -m django --version
Step 3: Start a New Django Project
With Django installed, you can now create your first Django project. A Django project is essentially a collection of settings and configurations that defines a web application.
Run this command: django-admin startproject myproject
This command creates a directory named myproject with the following structure:
myproject/
------manage.py
------myproject/
------------init.py
------------settings.py
------------urls.py
------------asgi.py
------------wsgi.py
manage.py: A command-line utility that lets you interact with this Django project.
settings.py: Configuration settings for the Django project.
urls.py: URL declarations for the Django project.
asgi.py and wsgi.py: Entry points for ASGI- and WSGI-compatible web servers.
Step 4: Run the Development Server
Before diving into the code, let's run the Django development server to see what a newly created project looks like.
Run this command in the terminal: python manage.py runserver
Open your web browser and go to https://127.0.0.1:8000/. You should see the default Django welcome page, which means everything is set up correctly.
Creating Your First Django App
A Django project is made up of applications that work together. Each app serves a specific function within the project, and you can reuse apps across different projects.
Step 1: Create a New App
Let’s create a simple app called blog. Run this command: python manage.py startapp blog
This command creates a new directory named blog with the following structure:
blog/
------migrations/
------init.py
------admin.py
------apps.py
------models.py
------tests.py
------views.py
领英推荐
models.py: Define the data structure.
views.py: Handle the logic and return responses.
admin.py: Register your models to make them available in the Django admin interface.
Step 2: Define Models
Models are the core of a Django application, representing the structure of your database. In blog/models.py, define a simple model for a blog post:
title: A short text field for the post title.
content: A text field for the post content.
created_at: A timestamp for when the post was created.
Step 3: Apply Migrations
Django uses migrations to propagate changes made to your models (like creating or modifying tables) into your database.
First, add your app to the INSTALLED_APPS list in myproject/settings.py:
Then, create and apply migrations. Run the commands below:
python manage.py makemigrations
python manage.py migrate
Step 4: Register Models in Admin
To manage your posts via the Django admin interface, register the Post model in blog/admin.py:
Create a superuser account so you can log in to the admin interface where you can manage the Posts data. Run this command in the terminal then follow the prompts: python manage.py createsuperuser
Run the development server and go to https://127.0.0.1:8000/admin/. Log in using the superuser account you just created.
Once logged in, you should see the Posts section where you can add, edit, and delete blog posts.
Creating Views and Templates
Django follows the MVT (Model-View-Template) architecture. We’ve covered Models, so now let’s create some Views and Templates to display our blog posts.
Step 1: Create Views
In blog/views.py, create a view that retrieves all posts from the database and renders them using a template:
Step 2: Create URLs
Map your view to a URL in blog/urls.py. First, create a urls.py file in your blog directory:
Then, include this URL configuration in the project's urls.py:
Step 3: Create Templates
In Django, templates define the structure of your HTML pages. Create a templates directory within your blog app, and inside it, create another blog directory (Django expects this structure). Then, create a post_list.html file:
Step 4: View Your Blog
Run the development server again: python manage.py runserver
Visit https://127.0.0.1:8000/ in your browser. You should see a list of blog posts displayed on the page.
Conclusion
Congratulations! You've just created a simple blog using Django. You've learned how to set up a Django project, create an app, define models, and display data using views and templates. This is just the beginning—Django offers many more features that you can explore as you continue to develop your skills.
For a more detailed walkthrough and additional tips, you can check out my YouTube video on Django basics where I cover these topics with live coding examples.
Stay tuned for more articles where we will dive deeper into Django's powerful features!