【问题标题】:Django: Render text and link from a textfield in databaseDjango:从数据库中的文本字段呈现文本和链接
【发布时间】: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


【解决方案1】:

更新从另一个页面转到另一个链接
如果您想呈现从当前页面到其他页面的链接。
试试这个

Some text I want to render in blog with a link <a href="http://localhost:8000/articles/7-Photogenic-Spiral-Staircases" class="link-within">8 Photogenic Spiral Staircases</a>

我实际上使用了实际的主机名(本地主机),这应该可以工作。

问题

它对您不起作用,因为您没有在链接中添加主机名,如果您不添加,那么 django 将采用当前 url + 您在该链接中写入的任何内容,它将无法理解这个变量{{ STATIC_URL }}.一个可能的解决方案是使用你想要在链接href中重定向的完整url

解决方案
模型.py

from django.urls import reverse
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)
    recommendations = models.ManyToManyField("Articles",null=True, blank=True)

    def get_absolute_url(self):
        return reverse('articles',args=[self.slug])

1)python manage.py makemigrations
2)python manage.py migrate
3)now inside your django admin you can add inside recommendations any article related to the current one.
4)now inside your redirect field you can just add a text saying like 'if you want to learn more about this topic please refer to'
模板:

    <p class="article-redirect">{{ article.redirect }} <br>
{% for i in article.recommendations.all %}
 <a href="{{ i.get_absolute_url }}">{{ i.title }}</a>
{% endfor %}
</p>

【讨论】:

  • 您好@amadou 感谢您的意见。是的,我想从博客文章链接到不同的页面。我希望能够在数据库中写入链接。如果我使用“localhost:8000”,我将不得不在生产中全部更改它们,对吗?我认为可能有更好的方法
  • @haashe 是的,您必须在生产中更改它们,这是正确的。
  • @haashe 你为什么要从一篇文章重定向到另一篇文章你是在实施推荐文章还是什么?
  • 类似推荐文章的东西。我想参考博文中的一些文章以获取有关主题的更多详细信息。
  • @haashe 让我想想我们该怎么做。
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 2011-01-02
  • 2010-11-24
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多