标签:har 访问 页面 tle 统一 www mod cascade register
model.py
文件中新增加Detail
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 支持所有常用的数据库关系:多对一、多对多和一对一
在管了后台添加数据
对应项目添加详情
重新改写一下project.html
的模板
<!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>
模板系统统一使用点符号来访问变量的属性
home
的页面,让对应内容可以跳转<!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>
home
页面 <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 %}
代替它
home.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 ‘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‘),
]
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>
标签:har 访问 页面 tle 统一 www mod cascade register
原文地址:https://www.cnblogs.com/yiluotalk/p/13584530.html