- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在写一个简单的应用程序博客。Django==2.0.4
我想创建人类可读的 URL。models.py
class Post(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
对于这个模型,我将添加 slug = models.SlugField('id_url', max_length=255, unique=True)
,对吧?
接下来,我将添加到网址
urlpatterns = [
path(r'', views.post_list, name='post_list'),
path(r'post/<str:slug>/', views.post_detail, name='post_detail'),
]
对吗?
但我不明白我需要添加到 views.py
中,并且需要或不需要我 def get_absolute_url(self)
在 models.py
中,我希望你能帮助我。谢谢。
最佳答案
在你的模型中你会想要
def get_absolute_url(self):
return '/post/%s' % (self.slug)
或
def get_absolute_url(self):
return redirect('post_detail', slug=self.slug)
在您的 View 中,您可以像平常一样编写:
def post_detail(request):
#do something
return render(request, 'post-detail.html', context)
关于python - 如何在 Django 中创建人类可读的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49954831/
有没有一种方法(程序、库)可以大致了解文档是用哪种语言编写的? 我有一堆混合语言的文本文档(~500K),需要导入支持 i18n 的 CMS (Drupal).. 我不需要完美的匹配,只需要一些猜测。
Closed. This question needs details or clarity。它当前不接受答案。
使用 UTF-8 编码。 (Multiple languages in one HTML page)。 跨不同浏览器(包括 iPad 上的 Safari)在单个网页上正确显示多种人类语言的最佳做法是什
我有一个完全可用的代码,它是为 Windows 编写的,是用 Visual Studio 构建的。 我想做的是为该软件添加另一种语言。我的想法是在窗口角落放置两个标志(一个英语和一个德语),并在用户点
我刚刚得到一个脚本,我想对其进行一些更改,我正在寻找某人为我开发一份自由职业,以使我将提供的加载字符串可读以进行编辑。 Lua代码是这样的: ------------------------- ENG
有没有不是简单逐字翻译的语言翻译项目?一个具有先进算法/设计的? 目前主流和流行的翻译软件,例如谷歌翻译,似乎是查找一个词或一组连续的词,然后将其直接翻译成不知何故确定为最佳匹配的内容。但是因为它不是
基于 Twilio 的人类/非人类检测 - 我正在尝试通过如下所示的代码来检测调用是否被人类或机器人接听 HashMap params = new HashMap(); params.put("Fro
这是目前(不确定)我能想到的处理多语言网站的最佳方式,它不涉及 gettext、zend_translate 或任何 php 插件或框架。 我认为它非常简单:我有 3 种语言,我将它们的“内容”写在不
我是一名优秀的程序员,十分优秀!