Welcome to the Django tutorial! Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. This video tutorial will guide you through the basics of setting up a Django project and building a simple web application.
Table of Contents
- Introduction to Django
- Setting Up Django
- Creating a Simple Django App
- Modeling Data with Django
- Creating Views and Templates
- Running the Development Server
- Further Reading
Introduction to Django
Django is an open-source web framework that follows the model-view-template (MVT) architectural pattern. It encourages rapid development and clean, pragmatic design. Django is used by many high-traffic sites such as Pinterest, Instagram, and Mozilla.
Setting Up Django
Before you start, make sure you have Python installed on your system. You can download it from python.org.
Install Django using pip:
pip install django
Create a new Django project:
django-admin startproject myproject
Navigate to the project directory:
cd myproject
Creating a Simple Django App
To create a new app within your project, use the following command:
python manage.py startapp myapp
This will create a new directory with the name myapp
containing the basic structure of a Django app.
Modeling Data with Django
Django uses models to define the data structure of your application. In myapp/models.py
, define a new model like this:
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
Run the following command to create the database tables:
python manage.py makemigrations
python manage.py migrate
Creating Views and Templates
Views are functions that handle the logic of your web application. In myapp/views.py
, create a new view like this:
from django.shortcuts import render
def my_view(request):
return render(request, 'myapp/my_template.html')
In myapp/templates/myapp/my_template.html
, create a simple template:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome to my Django app!</h1>
</body>
</html>
Running the Development Server
To run the development server, use the following command:
python manage.py runserver
Open your web browser and go to http://127.0.0.1:8000/
to see your Django app in action!
Further Reading
For more information on Django, check out the official documentation. If you have any questions or need help, join the Django community or visit the Django subreddit.
[center]