Reputation: 31
I am working on the Django blog. I have created a url column in the admin post as follows.
however when I click the link in the page, the page jumps to the link which contains the localhost:8000 at the head of the url. e.g. http://127.0.0.1:8000/Yahoo/www.yahoo.com
I made a link as <a href="{{ post.url }}">Visit yahoo.com!</a>
however this is probably a wrong way to do it.
Can you please advise me how to fix this issue?
my class in 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
my 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 content %}
Upvotes: 1
Views: 96
Reputation: 10699
http://127.0.0.1:8000/Yahoo/www.yahoo.com
This implies that the link you indicated was made relative to your current path:
<a href="{{ post.url }}">Visit yahoo.com!</a>
Making it absolute by adding a prefix slash would not help either as that would still reference to your current running server
<a href="/{{ post.url }}">Visit yahoo.com!</a>
http://127.0.0.1:8000/www.yahoo.com
If the post.url is external and outside your server, append a prefix of http://
or https://
<a href="http://{{ post.url }}">Visit yahoo.com!</a>
Upvotes: 1