- Django 2 by Example
- Antonio Melé
- 108字
- 2021-06-25 21:19:02
Canonical URLs for models
You can use the post_detail URL that you have defined in the preceding section to build the canonical URL for Post objects. The convention in Django is to add a get_absolute_url() method to the model that returns the canonical URL of the object. For this method, we will use the reverse() method that allows you to build URLs by their name and passing optional parameters. Edit your models.py file and add the following:
from django.urls import reverse
class Post(models.Model):
# ...
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.month,
self.publish.day,
self.slug])
We will use the get_absolute_url() method in our templates to link to specific posts.
推薦閱讀
- 網絡云百問百答
- MERN Quick Start Guide
- Building E-commerce Sites with VirtueMart Cookbook
- 農產品物聯網研究與應用
- 面向物聯網的嵌入式系統開發:基于CC2530和STM32微處理器
- React:Cross-Platform Application Development with React Native
- 雷達饋線技術
- Learning Swift(Second Edition)
- SAE原理與網絡規劃
- 深入理解Nginx:模塊開發與架構解析
- bash網絡安全運維
- RestKit for iOS
- Hands-On Reactive Programming in Spring 5
- 網絡基本通信約束下的系統性能極限分析與設計
- 萬物互聯:物聯網核心技術與安全