Learn Django Framework from Scratch: Practical Tutorials and Examples
Django is a popular Python web application framework that simplifies the website development process. It provides a powerful set of tools and libraries to help developers build efficient, scalable and secure web applications. For beginners, learning Django may be difficult, but through some practical tutorials and examples, you can quickly get started and understand the core concepts and usage of this framework.
This article will take you step by step to learn the Django framework and provide some specific code examples, allowing you to build a simple web application from scratch.
First, you need to install Python and Django. Make sure you have installed Python correctly and enter the following command at the command line to install Django:
pip install django
Next, we will create a new Django project. Enter the following command at the command line:
django-admin startproject mysite
This will create a new project named "mysite". Go into the project folder and run the following command to start the development server:
cd mysite python manage.py runserver
Now you can open "http://localhost:8000" in your browser to view your application.
Next, we will create a simple application. Run the following command in the command line:
python manage.py startapp myapp
This will create a new application named "myapp". To make our app known to Django, open the "settings.py" file in the project folder and add "myapp" to the "INSTALLED_APPS" list.
Now, we will create a simple view. In the application's folder, open the "views.py" file and add the following code:
from django.http import HttpResponse def hello(request): return HttpResponse("Hello, world!")
Next, we need to create a URL mapping in order to send requests to our view functions. In the application's folder, create a new file called "urls.py" and add the following code:
from django.urls import path from . import views urlpatterns = [ path('', views.hello, name='hello'), ]
Now, we need to add this URL mapping to the project's "urls.py" in the file. In the project's folder, open the "urls.py" file and add the following code:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('hello/', include('myapp.urls')), ]
Now you can restart the development server and open "http://localhost:8000/" in your browser hello" to view your application.
In addition to simple views, Django also provides powerful model and database interaction functions. We can create a simple model to store and retrieve data. In the application's folder, open the "models.py" file and add the following code:
from django.db import models class Book(models.Model): title = models.CharField(max_length=200) author = models.CharField(max_length=200) def __str__(self): return self.title
Next, we need to create a table for this model in the database. Run the following command on the command line:
python manage.py makemigrations python manage.py migrate
Now, we can add, edit, and delete book records through Django's admin interface. In order to enable the admin interface, we need to create a new file called "admin.py" in the application's folder and add the following code:
from django.contrib import admin from .models import Book admin.site.register(Book)
Now we can restart the development server and access " http://localhost:8000/admin" to log in to the administrator account and manage book records.
Through the above practical tutorials and examples, you have already understood the basic usage and core concepts of the Django framework. You can continue to learn advanced features such as Django's template system, forms, and authentication capabilities to build more complex web applications.
Hope this tutorial will be helpful to your Django learning journey! I wish you success!
The above is the detailed content of Learn the Django framework from scratch: practical tutorials and examples. For more information, please follow other related articles on the PHP Chinese website!