Summary of "Django Tutorial #3 - Django Basics"
Summary of “Django Tutorial #3 - Django Basics”
This tutorial covers the foundational steps for setting up a Django project and application, focusing on creating a simple “Asia Tours Agency” management system. The video explains the project structure, key files, and how to render a basic HTTP response in the browser. It also introduces URL routing and setting up the project to run on a local server.
Main Ideas and Concepts
Project and Application Setup
- Navigate to the desired directory and activate the Python virtual environment (
pnv shell). - Install Django inside the virtual environment using
pnv install django. -
Create a new Django project using the command:
django-admin startproject WorldTour -
Change directory into the project folder (
WorldTour). - Create a new Django application inside the project with:
python manage.py startapp AsiaToursAgency
Virtual Environment and Dependency Management
- Virtual environments isolate project dependencies from the global Python environment.
Pipfileis auto-generated to track dependencies and Python version, similar topackage.jsonorCargo.toml.- Avoid manually editing the
Pipfile.
Django Application File Overview
__init__.py: Marks the directory as a Python package (can be empty).admin.py: Register models to make them accessible via Django’s admin interface.apps.py: Contains the application configuration class (e.g.,AsiaToursAgencyConfig) which stores app-specific settings.models.py: Defines data models representing database tables and fields.tests.py: For writing unit tests for the app.views.py: Contains the logic to handle HTTP requests and return responses (the “View” in Django’s MVT architecture).migrations/: Folder containing migration files that track changes to models and database schema.
Linking Application to Project
- Add the application to the project’s
settings.pyunderINSTALLED_APPS. - Two ways to add:
- By app name string:
'AsiaToursAgency' - By referencing the config class:
'AsiaToursAgency.apps.AsiaToursAgencyConfig'(preferred)
- By app name string:
Creating a Basic View and HTTP Response
- Import
HttpResponsefromdjango.http. - Define a simple view function
index(request)that returns a plain text HTTP response, e.g.,"Asia Tours Agency". - This is a placeholder before creating proper HTML templates.
URL Routing
- Each Django app can have its own
urls.pyfile to define routes. - The project has a main
urls.pywhich includes app-level URLs usinginclude(). - Import
pathandincludefromdjango.urls. - Map the root URL (
'') to theindexview in the app’surls.py. - Include the app’s URL patterns in the project-level
urls.py.
Running the Server
- Ensure you are in the directory containing
manage.py. - Run the server with
python manage.py runserver. - Access the app at
http://localhost:8000/. - The page currently renders a simple string, not a full HTML template.
Next Steps
In future lessons, the tutorial will cover:
- Creating HTML templates for rendering proper web pages.
- Working with databases and migrations.
- Adding models and displaying data via Django shell and browser.
Methodology / Step-by-Step Instructions
-
Set up environment and install Django
-
Navigate to project directory:
cd lesson2_django_app -
Activate virtual environment:
pnv shell -
Install Django:
pnv install django
-
-
Create Django project and application
-
Create project:
django-admin startproject WorldTour -
Change directory:
cd WorldTour -
Create app:
python manage.py startapp AsiaToursAgency
-
-
Register the application
- Open
settings.py - Add
'AsiaToursAgency.apps.AsiaToursAgencyConfig'toINSTALLED_APPS
- Open
-
Create a simple view In
AsiaToursAgency/views.py:
```python from django.http import HttpResponse
def index(request): return HttpResponse(“Asia Tours Agency”) ```
- Set up URL routing
Create
AsiaToursAgency/urls.py:
```python from django.urls import path from . import views
urlpatterns = [ path(‘’, views.index, name=’index’), ] ```
Modify project urls.py:
```python from django.contrib import admin from django.urls import path, include
urlpatterns = [ path(‘admin/’, admin.site.urls), path(‘’, include(‘AsiaToursAgency.urls’)), ] ```
-
Run the server
-
Run:
python manage.py runserver -
Open browser at
http://localhost:8000/to see the output.
-
Speakers / Sources
- Primary Speaker: The tutorial instructor (name not provided) guiding through the Django basics and project setup.
This summary encapsulates the core lessons and instructions on starting a Django project, creating an application, understanding the project structure, setting up views and URLs, and running the development server.
Category
Educational
Share this summary
Is the summary off?
If you think the summary is inaccurate, you can reprocess it with the latest model.