Python圈Python基础

Django基础7--模板系统

2020-08-30  本文已影响0人  伊洛的小屋
1.使用模板系统
from django.db import models

# Create your models here.


class ProjectInfo(models.Model):
    """ 项目信息模型 """
    project_name = models.CharField(max_length=30)
    dev = models.CharField(max_length=20)
    tester = models.CharField(max_length=20)
    add_data = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        # 返回项目名称
        return self.project_name


class Detail(models.Model):
    project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    detail_text = models.CharField(max_length=200)

    def __str__(self):
        return self.detail_text

使用 ForeignKey定义了一个关系。这将告诉 Django,每个 detail 对象都关联到一个 ProjectInfo 对象。Django 支持所有常用的数据库关系:多对一、多对多和一对一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AutoPlarform Home</title>
</head>
<body>
<h1>{{ project.project_name }}</h1>
{% for detail in project.detail_set.all %}
    <li>{{ detail.detail_text }}</li>
{% endfor %}
</body>
</html>

模板系统统一使用点符号来访问变量的属性

2. 去除模板中的硬编码URL
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AutoPlarform Home</title>
</head>
<body>
{% if project_list %}
    <ul>
    {% for project in project_list %}
        <li>
        <a href="/autoapi/{{ project.id }}/">{{ project.project_name }}</a>
        </li>
    {% endfor %}
    </ul>
{% endif %}

</body>
</html>
 <a href="/autoapi/{{ project.id }}/">{{ project.project_name }}</a>

硬编码和强耦合的链接,对于一个包含很多应用的项目来说,修改起来是十分困难的
不过的还记得之前在url配置页面的内容嘛

# 作者:伊洛Yiluo 公众号:伊洛的小屋
# 个人主页:https://yiluotalk.com/
# 博客园:https://www.cnblogs.com/yiluotalk/
from django.urls import path
from . import views


urlpatterns = [
    path('home/', views.home, name='index'),
    path('<int:project_id>/', views.project_list, name='project list'),
    path('register/', views.register, name='register'),
]

之前我们通过name参数为url定义了名字,现在我们可以通过{% url %}代替它

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AutoPlarform Home</title>
</head>
<body>
{% if project_list %}
    <ul>
    {% for project in project_list %}
        <li>
        <a href="{% url 'project list' project.id %}">{{ project.project_name }}</a>
        </li>
    {% endfor %}
    </ul>
{% endif %}

</body>
</html>
from django.urls import path
from . import views


urlpatterns = [
    path('home/', views.home, name='index'),
    path('detail/<int:project_id>/', views.project_list, name='project list'),
    path('register/', views.register, name='register'),
]

3.为URL名称添加命名空间

现在再Django项目中,只有一个APP,但是实际开发过程中会有很多。Django如何分辨重名的URL
答案就是:在URLconf中添加命名空,加上app_name 设置命名空间

from django.urls import path
from . import views


app_name = 'autoapi'
urlpatterns = [
    path('home/', views.home, name='index'),
    path('detail/<int:project_id>/', views.project_list, name='project list'),
    path('register/', views.register, name='register'),
]

HTML页面再改动下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AutoPlarform Home</title>
</head>
<body>
{% if project_list %}
    <ul>
    {% for project in project_list %}
        <li>
        <a href="{% url 'autoapi:project list' project.id %}">{{ project.project_name }}</a>
        </li>
    {% endfor %}
    </ul>
{% endif %}

</body>
</html>

欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读