Home > Backend Development > Python Tutorial > Understanding the Django framework: from entry to mastery

Understanding the Django framework: from entry to mastery

PHPz
Release: 2024-01-19 10:45:05
Original
1350 people have browsed it

Understanding the Django framework: from entry to mastery

Django is a popular Python web framework that provides an efficient and powerful way to develop web applications. This article will introduce the basic knowledge of Django from entry to master, and provide specific code examples.

  1. Installing Django

Before using Django, you need to make sure that Python is installed on your computer. Then, you can install Django using the following command:

pip install Django
Copy after login
  1. Create Django Project

To create a new Django project, you can use the following command:

django-admin startproject <projectname>
Copy after login

For example, to create a project named "mysite", execute the following command:

django-admin startproject mysite
Copy after login
  1. Run Django Server

To run the Django server, enter the project file folder and use the following command:

python manage.py runserver
Copy after login

By default, the server will run on your local machine and listen on port 8000.

  1. Create a Django application

In a Django project, an application is the basic component of a web application. A new application can be created using the following command:

python manage.py startapp <appname>
Copy after login

For example, to create an application named "blog", execute the following command:

python manage.py startapp blog
Copy after login
  1. Write a Django view

Django views are code in a web application that handle requests from users and return responses. They can be written as functions or classes. The following is a simple Django view example:

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, world.")
Copy after login

In this example, the view function "hello" receives an HTTP request and returns an HTTP response containing "Hello, world."

  1. Writing Django templates

Django's template is an HTML file that can use Django's template language to render dynamic content. Here is a simple template example:

<!doctype html>
<html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>{{ heading }}</h1>
        <p>{{ content }}</p>
    </body>
</html>
Copy after login

In this example, "title", "heading" and "content" are variables and their values ​​can be set in the view.

  1. Define Django URL

In Django, a URL is an address used in a web application to which a view function will respond. You can use the following code to define a URL for a view function:

from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello),
]
Copy after login

In this example, the URL "/hello/" will point to the view function "hello".

  1. Connect to the database

Django provides an ORM (Object Relational Mapping) that supports a variety of databases. With an ORM, you can use Python code to interact with the database without writing raw SQL queries. Here is a simple example of using an ORM to create a database table:

from django.db import models

class BlogPost(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
Copy after login

In this example, "BlogPost" is a Django model that will be mapped to a table in the database. It defines three fields: "title", "content" and "pub_date".

  1. Run Django Data Migration

Once the model is defined, you can create the relevant tables in the database using the following command:

python manage.py makemigrations
python manage.py migrate
Copy after login

First command A migration script will be created and the second command will apply the script and update the database.

  1. Manage using Django

Django comes with a powerful management site that can be used to manage data in web applications. Create an administrator account with the following command:

python manage.py createsuperuser
Copy after login

You can then log in to the admin site by accessing "/admin/" in a web browser.

To sum up, Django provides an efficient and powerful way to develop web applications. This article covers the basics of Django, including how to create a Django project, write Django views and templates, define Django URLs, connect to a database, run Django data migrations, and manage a site using Django. By understanding and practicing these basics, you can go from getting started with Django to becoming proficient.

The above is the detailed content of Understanding the Django framework: from entry to mastery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template