【发布时间】:2021-12-30 17:32:34
【问题描述】:
我是网络开发的新手,目前正在建立一个博客。 我正在尝试呈现我在数据库内的文本字段中编写的一些文本。 问题是该文本包含一个链接到另一篇博客文章的链接。
This is the textfield from "redirect" in Articles model
Some text I want to render in blog with a link <a href="{{ STATIC_URL }}/articles/8-Photogenic-Spiral-Staircases" class="link-within">8 Photogenic Spiral Staircases</a>
html
<p class="article-redirect">{{ article.redirect|safe|escape }}</p>
使用 |safe 过滤器成功呈现 HTML,但是当我单击链接时显示 404 错误,错误消息中请求的 URL 是
Request URL: http://localhost:8000/articles/7-Photogenic-Spiral-Staircases%7B%7B%20STATIC_URL%20%7D%7D/articles/8-Photogenic-Spiral-Staircases
在上面请求的导致错误的 URL 中,“7-Photogenic-Spiral-Staircases”是当前的博客文章,“8-Photogenic-Spiral-Staircases”是我想要跳转到的博客文章。
谁能帮我理解为什么这不起作用以及可能的解决方案是什么?
models.py
class Articles(models.Model):
title = models.CharField(max_length=155)
slug = models.SlugField(unique=True, max_length=155)
summary = models.TextField(blank=True, null=True)
redirect = models.TextField(blank=True, null=True)
views.py
def article(request, slug):
article = get_object_or_404(Articles, slug=slug)
context = {
'article': article,
}
return render(request, 'articletemplate.html', context)
urls.py
urlpatterns = [
path("articles/<str:slug>/", views.article, name="articles")
]
【问题讨论】:
-
我更新了我的答案试试看。
标签: python django django-models django-views django-templates