Table of Contents
Define the general view
自定义的404页面
Home Backend Development Python Tutorial How to use Python Django's generic views and error views?

How to use Python Django's generic views and error views?

May 08, 2023 pm 09:49 PM
python django

Define the general view

Modify the AuthorInfo class in the book/models.py code. If it is consistent, there is no need to modify it

class AuthorInfo(models.Model):
    id = models.CharField(max_length=30, verbose_name="身份证号", primary_key=True)
    name = models.CharField(max_length=20, verbose_name="姓名")
    telephone = models.CharField(max_length=20, verbose_name="联系方式")
    age = models.IntegerField(verbose_name="年龄", default=30)
    sex = models.CharField(max_length=2, verbose_name="性别", default="男")

    def __str__(self):
        return self.name
Copy after login

inbook/views.py Create a new AuthorListView function under the file

from book.models import AuthorInfo
from django.views.generic.list import ListView

class AuthorListView(ListView):
    model = AuthorInfo
    template_name = "list.html"
    context_object_name = "my_author"
Copy after login

How to use Python Djangos generic views and error views?

## in

book/urls.py The urlpatterns Create a new route in the list

path('author/', views.AuthorListView.as_view())
Copy after login

How to use Python Djangos generic views and error views?

New

templates/list.html File

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<ul>
    {% for item in my_author %}
    <li>{{ item.name }}</li>
    {% endfor %}
</ul>
</body>
</html>
Copy after login

Access http://127.0.0.1:8000/book/authorlist/

If you cannot access the page, please check the

urlpatterns list in the chapter1/urls.py file Does it contain the route of book

How to use Python Djangos generic views and error views?

If there is no error and the page is blank, please check whether the database contains data. The following is how to add sample data Code

INSERT INTO book_authorinfo (id, name, telephone, age, sex) VALUES
(&#39;a001&#39;, &#39;Alice&#39;, &#39;13812345678&#39;, 25, &#39;F&#39;),
(&#39;a002&#39;, &#39;Bob&#39;, &#39;13987654321&#39;, 30, &#39;M&#39;),
(&#39;a003&#39;, &#39;Charlie&#39;, &#39;13611112222&#39;, 40, &#39;M&#39;),
(&#39;a004&#39;, &#39;David&#39;, &#39;13533334444&#39;, 20, &#39;M&#39;),
(&#39;a005&#39;, &#39;Eve&#39;, &#39;13755556666&#39;, 35, &#39;F&#39;);
Copy after login

can be executed here

How to use Python Djangos generic views and error views?

If there is no problem, you will see the author information

How to use Python Djangos generic views and error views?

Define error view template

Modify

chapter1/settings.py File

DEBUG = False

ALLOWED_HOSTS = [&#39;*&#39;]
Copy after login

How to use Python Djangos generic views and error views?

New

templates/404 .html file

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>此页面未找到</title>
</head>
<body>
    <h3 id="自定义的-页面">自定义的404页面</h3>
    <p>您访问的页面不存在</p>
</body>
</html>
Copy after login
When entering the undefined routing URL, the webpage written above will be displayed

How to use Python Djangos generic views and error views?

The above is the detailed content of How to use Python Django's generic views and error views?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

What software is NET40? What software is NET40? May 10, 2024 am 01:12 AM

What software is NET40?

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

How to search deepseek

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

What language is the browser plug-in written in?

See all articles