Introduction to Django: Basic Syntax and Structure
1. Introduction
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This tutorial will guide you through the basic syntax and structure of a Django project.
2. Setting Up a Django Project
First, ensure you have Django installed. You can install Django using pip:
pip install django
Once Django is installed, you can create a new project using the following command:
django-admin startproject myproject
This command will create a directory structure as shown below:
myproject/ manage.py myproject/ __init__.py settings.py urls.py wsgi.py
3. Project Structure
Let's break down the files and directories created in a new Django project:
- manage.py: A command-line utility that lets you interact with this Django project.
- myproject/: The inner directory containing the actual project.
- __init__.py: An empty file that tells Python that this directory should be considered a Python package.
- settings.py: Settings/configuration for this Django project.
- urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site.
- wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
4. Creating a Django Application
Within a project, you can create multiple applications. Each application should do one thing. To create a new application, use the following command:
python manage.py startapp myapp
This command creates a directory structure as shown below:
myapp/ __init__.py admin.py apps.py models.py tests.py views.py migrations/ __init__.py
5. Application Structure
Let's break down the files and directories created in a new Django application:
- __init__.py: An empty file that tells Python that this directory should be considered a Python package.
- admin.py: Configuration for the Django admin interface.
- apps.py: Configuration for the application itself.
- models.py: Database models used by this application.
- tests.py: Unit tests for this application.
- views.py: Web views for this application.
- migrations/: Database migration files.
6. Configuring the Application
Once you create an application, you need to tell your Django project about it. Open settings.py and add your new application to the INSTALLED_APPS list:
INSTALLED_APPS = [ ... 'myapp', ]
7. Writing Views
Views are Python functions or classes that receive a web request and return a web response. Open views.py in your application directory and add a simple view:
from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the myapp index.")
8. Configuring URLs
To call the view, you need to map it to a URL. Create a new file named urls.py in your application directory and add the following code:
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
Then, include this URL configuration in the main URL configuration of your project. Open urls.py in the project directory and add the following code:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]
9. Running the Server
Now you can run the development server to see your application in action. Use the following command:
python manage.py runserver
Open your web browser and go to http://127.0.0.1:8000/myapp/ to see your application running.
10. Conclusion
In this tutorial, we covered the basic syntax and structure of a Django project. We learned how to set up a Django project, create an application, configure URLs, and write views. Django's structure encourages clean, maintainable code and rapid development.