O
O
Ozrae2020-05-11 14:04:25
Django
Ozrae, 2020-05-11 14:04:25

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')


My views.py:
def posts_list(request):
    posts = Post.objects.all().order_by('date')
    return render(request, 'blog/index.html', {'posts': posts})


And html:
{% 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 %}


I create posts, but their placement on the site does not work, if you enter model variables like {{ post.title }}, there will be a void in their place. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan Krapivin, 2020-05-11
@Ozrae

on the page you start the cycle

{% for article in posts %}
...
{% endfor %}

in each iteration of the loop, the article object is available - this is your Post
{% for article in posts %}
  {{ article.title }}
{% endfor %}

But what kind of game is this? and Where does the Post object come from in the template and where does the post object come from? You didn't define them anywhere. There is only article
<h1>{{ Post.title }}</h1>
{{ post.likes }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question