Build Your First Django Web App: Step‑by‑Step Guide for Beginners
This tutorial walks you through Django fundamentals, including its MTV architecture, project and app creation, URL routing, views, templates, static files, form handling, and database integration, enabling you to build a functional web service from scratch.
1. Introduction to Django
1. Web framework overview
Before introducing Django, we need to explain the concept of a web framework. A web framework is a pre‑defined website template that you fill or modify to suit your needs.
Typical web framework architecture is shown below:
Other Python web frameworks such as Tornado, Flask, and WebPy are variations of this architecture; Tornado uses its own asynchronous WSGI, Flask provides a minimal core, while Django uses WSGI and implements most functionality out of the box.
2. MVC/MTV explanation
MVC (Model‑View‑Controller) separates business logic, data, and presentation. In simple terms, it organizes files into separate directories.
Model : defines database‑related content, usually placed in models.py.
View : defines static files such as HTML, CSS, and JavaScript.
Controller : contains the business logic.
Some frameworks rename MVC to MTV, where the “view” becomes the controller and HTML templates are stored in a templates directory.
3. Django’s MTV organization
Django uses urls, ORM, static files, and settings to connect the separated components. A typical workflow is illustrated below:
2. Django project example
1. Installation
Install Python 3.5, pip3, and PyCharm Professional (the free edition does not support Django).
Installation of Django
Download from http://www.cnblogs.com/qianyuliang/p/6729298.html .
2. Create a Django project
Use Django commands in a Linux terminal or Eclipse. In Eclipse choose File → Project, select PyDev/Django, and name the project “mysite”.
Django automatically generates the following directory structure:
The project‑named directory contains configuration files; the templates directory holds HTML files; manage.py is the project management script.
3. Create an app
Each Django project can contain multiple apps, which are relatively independent sub‑modules sharing project resources.
Right‑click mysite → Django → Create application to generate an app folder (e.g., app01).
4. Define URLs
All routes are defined in urls files, mapping browser URLs to business logic.
5. Write business logic
Business logic resides in views.py.
Mapping the “index” URL to the index() function returns a “hello world” string.
6. Run the web service
Remember to add app01 to settings.py, then start the server with python manage.py runserver 127.0.0.1:8000. In Eclipse use Run Configurations.
After a successful start, open the browser at 127.0.0.1:8000.
7. Return HTML files
Instead of a plain string, return an HTML file (e.g., index.html).
8. Use static files
Place CSS, JS, and plugins in a static directory and configure settings.py accordingly.
9. Receive user data
Add a form to index.html for username and password, submit to the index URL, and handle the data in views.py. Disable CSRF protection in settings.py for this demo.
10. Return dynamic pages
Use Django’s template language (similar to Jinja2) to render data dynamically.
11. Use a database
Configure a MySQL database (or use the default SQLite). Define models with fields for name and password, then run python manage.py makemigrations and python manage.py migrate. Update views.py to store and retrieve data.
After restarting the server, user data is persisted in the database.
3. Summary
Django is a powerful, comprehensive Python web framework. While it imposes certain constraints, mastering its core principles—MTV architecture, routing, views, templates, static files, and ORM—allows you to build full‑featured web applications efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
