Answer the question
In order to leave comments, you need to log in
How to make a post create function in django?
I have a jumbotron html element and a card below it. I need to make it so that when you create a post it is placed in jumbotron and when you create another post it will be shifted down to the first card, that is, sorted from new to old.
Here is my models.py:
class Post(models.Model):
title = models.CharField(max_length=65)
text = models.CharField(max_length=150)
date = models.DateField(blank='true', auto_now_add=True)
image = models.FileField(blank='true')
tag = models.CharField(max_length=50, blank='true')
likes = models.IntegerField(blank=True, default='0')
comm = models.IntegerField(blank=True, default='0')
views = models.IntegerField(blank=True, default='0')
def posts_list(request):
posts = Post.objects.all().order_by('date')
return render(request, 'blog/index.html', {'posts': posts})
{% block content %}
{% for article in posts %}
<div class='new'>
<div class='new-post'>
<ul class='new-post-status'>
<li><h4>date</h4></li>
<li><h5>{{ post.likes }}</h5></li>
</ul>
<div class='new-post-description'>
<h1>{{ Post.title }}</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore, alias.</p>
<a>Читать</a>
</div>
</div>
<img class='new-img' src='../../../static/images/1Statya_3.png'>
</div>
<div class='content'>
<div class='content-nav'>
<p href="#">Другие статьи</p>
<a href="#">FILTERS</a>
</div>
<div class='cards-container'>
<div class='card'>
<p class='pub-date'>date</p>
<img class='card-img' src='../../../static/images/1Statya_2.png'>
<p class='card-tag'>tag</p>
<p class='card-text'>title</p>
<div class='statistics'>
<img class='like-img' src='../../../static/images/Vector.png'>
<p class='likes-count'>{{ post.likes }}</p>
<img class='comment-img' src='../../../static/images/Vector (1).png'>
<p class='comments-count'>10</p>
<img class='view-img' src='../../../static/images/Vector (2).png'>
<p class='views-count'>10</p>
</div>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
Answer the question
In order to leave comments, you need to log in
on the page you start the cycle
{% for article in posts %}
...
{% endfor %}
{% for article in posts %}
{{ article.title }}
{% endfor %}
<h1>{{ Post.title }}</h1>
{{ post.likes }}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question