'Display blog Tittle, Created Date and Body from database in a HTML page using Django
My database table :
class Post(models.Model):
title = models.CharField(max_length=100)
body = models.CharField(max_length=1000000)
created_at = models.DateTimeField(default=datetime.now, blank=True)
My view :
def post(request, pk):
posts = Post.objects.filter(title=pk.replace('-', ' '))
blog_content = Post.objects.all()
return render(request, 'posts.html', {'posts': posts}, {'blog_content': blog_content})
My template :
{% block content %}
<section class="blog-list px-3 py-5 p-md-5">
<div class="container">
<div class="item mb-5">
<div class="media">
<div class="media-body">
<h1 class="title mb-1"><a style = "text-decoration:none;">{{blog_content.title}}</a></h1>
<div class="meta mb-1"><span class="date">{{blog_content.body}}</span></div>
<div class="intro">{{blog_content.created_at}}</div>
</div><!--//media-body-->
</div><!--//media-->
</div><!--//item-->
</div>
</section>
{% endblock %}
When I click an individual blog title from the home page it should show that post title, created date, and body on the posts.html page. But its showing raw html and css tags ! What's the solution ?
Here is the index( home page ) page details :
View :
def index(request):
posts = Post.objects.all()
return render(request, 'index.html', {'posts': posts})
Templates :
<div class="media">
<div class="media-body">
{% for post in posts reversed %}
<h3 class="title mb-1"><a style = "text-decoration:none;" href="{% url 'post' post.title|slugify %}">{{post.title}}</a></h3>
<div class="meta mb-1"><span class="date">{{post.created_at}}</span></div>
<div class="intro">{{post.body|truncatewords:30}}</div>
<a class="more-link" href="{% url 'post' post.title|slugify %}">Read more →</a>
{% endfor %}
</div><!--//media-body-->
</div><!--//media-->
Solution 1:[1]
Perhaps these two issues lead to the error you are seeing:
One, your anchor tag has no href therefore it will not link to another page.
<a style = "text-decoration:none;">{{blog_content.title}}</a>
Should be,
<a href="{% url 'posts' %}" style="text-decoration:none;">{{blog_content.title}}</a>
(I'm just guessing that you want to link to your posts view)
Second, this line looks suspicious:
posts = Post.objects.filter(title=pk.replace('-', ' '))
Since you have not declared a primary key for your Post model, Django automatically creates integer primary keys (pk), so filtering the title field of Post by replacing a non-existent - with a space makes no sense to me.
Edit
Try replacing,
<a style = "text-decoration:none;" href="{% url 'post' post.title|slugify %}">{{post.title}}</a>
<!-- WITH -->
<a style = "text-decoration:none;" href="{% url 'post' post.pk %}">{{post.title}}</a>
Then in your posts view access it by:
posts = Post.objects.filter(pk=pk)
This might mean you have to modify the urls.py:
path('post/<int:pk>', views.post, name='post'),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
