[Python+Django]初心者笔记8(从数据明细页做foreign key外部超链接)

时间:2019-09-12
本文章向大家介绍[Python+Django]初心者笔记8(从数据明细页做foreign key外部超链接),主要包括[Python+Django]初心者笔记8(从数据明细页做foreign key外部超链接)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

[Python+Django]初心者笔记8(从数据明细页做foreign key外部超链接)


想要在Book的明细页,加入foreign key的作者的外部超链接的话,依照以下步骤即可:
在catalog/urls.py的urlpatterns加入这段:增加author-detail的 url mapping机制

#加入Author数据表的详细数据网页的url mapping
path('author/', views.AuthorDetailView.as_view(), name='author-detail'),

在catalog/views.py的最下面加入这段:增加author-detail的server side取db数据的行为

class AuthorDetailView(generic.DetailView):
    """
    Generic class-based detail view for an author.
    """
    model = Author    

新增这个文件/locallibrary/catalog/templates/catalog/author_detail.html,.html档内容如下:

{% extends "base_generic.html" %}

{% block content %}
  

Author: {{ author.first_name }}

first_name: {{author.first_name}}

last_name: {{author.last_name}}

date_of_birth: {{author.date_of_birth}}

date_of_death: {{author.date_of_death}}

Author's Books

{% for book in author.book_set.all %}

title: {{book.title}}

summary:{{book.summary}}

{% endfor %}
{% endblock %}

book_detail.html里面的一个author的超链接,原本是空的超链接,改成下面这样:

Author: {{ book.author }}

这样就可以正确显示了author_detail:

这篇大概是这样…


参考数据:
Django Tutorial Part 6: Generic list and detail views
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views

原文:大专栏  [Python+Django]初心者笔记8(从数据明细页做foreign key外部超链接)


原文地址:https://www.cnblogs.com/chinatrump/p/11513063.html