Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

html - How to put each loop value into separate divs in django

enter image description here

Here's my html page. I want each 'question' to be placed in separate div to make them look nice. So I put in the following code:

{% for post in object_list %}
    <div class="myDiv">
    <a href="{% url 'article' post.pk %}">{{ post.title }}</a> <br>- {{ post.author }} , {{ post.date.date }}<br>
    </div>
{% endfor %}

But here's what it did:

enter image description here

It makes one big div and placing all the posts/questions in them. How do I separate them?

Here's my css:

         .myDiv {
          background-color: #012B39;
          text-align: left;
          margin: left;
          width: 25%;
          }

Little help will be appreciated! THANKS

P.S I will change the font colors to make them visible with the div.

question from:https://stackoverflow.com/questions/65923284/how-to-put-each-loop-value-into-separate-divs-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not that all the elements are rendered in one div. Your div's simply don't have any bottom / top margin and with the background color, It's looking like one div tag. You should check in the inspect element in your browser to make sure. A simple fix would be to add some bottom / top margin in your css:

.myDiv {
    background-color: #012B39;
    text-align: left;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    width: 25%;
}

Adjust the margin as per your convenience.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...