【发布时间】:2020-05-26 14:26:56
【问题描述】:
我正在开发 Django 博客。我在管理帖子中创建了一个 url 列,如下所示。
但是,当我单击页面中的链接时,页面会跳转到 URL 开头包含 localhost:8000 的链接。 例如http://127.0.0.1:8000/Yahoo/www.yahoo.com
我将链接设为<a href="{{ post.url }}">Visit yahoo.com!</a>,但这可能是一种错误的做法。
你能告诉我如何解决这个问题吗?
model.py 中我的类
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
url = models.CharField(max_length=200, unique=True, default='')
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
我的html
{% extends 'base.html' %} {% block content %}
<div class="container">
<div class="row">
<div class="col-md-8 card mb-4 mt-3 left top">
<div class="card-body">
<h1>{% block title %} {{ object.title }} {% endblock title %}</h1>
<p class=" text-muted">{{ post.author }} | {{ post.created_on }}</p>
<p class=" text-muted">{{ post.url }}</p>
<a href="{{ post.url }}">Visit yahoo.com!</a>
<p class="card-text ">{{ object.content | safe }}</p>
</div>
</div>
{% block sidebar %} {% include 'sidebar.html' %} {% endblock sidebar %}
</div>
</div>
{% endblock 内容 %}
【问题讨论】:
-
您需要在开头添加 url 方案(http:// 或 https://)位,以便浏览器能够确定 URL 不是相对的网站。
-
酷!有效。非常感谢
标签: django