Telemachos
Telemachos

Reputation: 11

How do I assign an ID to a div using Django?

I'm using mezzanine, based on the django framework of Python and I'm trying to add a method for my blog posts. In the models.py I have added an incremental counter to be used as an id reference.

`

def get_new_id():
        n = 100
        i = 1
        for i in range(n):
            if i ==100:
                break
            print (i) ##just checking if it actually did what I asked
            return i

This way I want to be able to show the newest post on my homepage without having to change it manually. I'd probably still need to reverse the count so the last blogpost becomes number 1, but regardless, the issue is not that.

I understand the code might already be too little for what I want it to do but even this simple loop does not print i in the console, let alone place itself into the meta tag. I have added the reference to the method in the correct html page (or so I believe)

from blog_post_list.html

    {% for blog_post in blog_posts.object_list %}

 <div id="{{ blog_post.get_new_id }}" class="post-preview">
          <a href="{{ blog_post.get_absolute_url }}">
            <h2 class="post-title">
              {{ blog_post.title }}
            </h2>
              </a>
                {% if settings.BLOG_USE_FEATURED_IMAGE and blog_post.featured_image %}
                {% block blog_post_list_post_featured_image %}
                <a href="{{ blog_post.get_absolute_url }}">
                <img class="img-thumbnail" src="{{ MEDIA_URL }}{% thumbnail blog_post.featured_image 730 474 %}">

I am wondering if it is just the method that is poorly written or if I'm doing something else wrong entirely. If I can assign an ID to every blogpost I could use it anywhere on my website which would be awesome. I'm a novice still so I appreciate any help that you can muster. For the moment, when I run this code, the ID is NOT implemented in the html, rather the entire method is just ignored. the id meta tag of the div remains empty. However ,from the same page (blog_post_list.html) other methods are invoked and do work properly in the same fashion so the link is there.

Thanks in advance

Upvotes: 1

Views: 276

Answers (1)

moddayjob
moddayjob

Reputation: 708

If you would like to give an id to each item rendered in the for loop, and you would like your id to increment you can simply add a Django for loop counter. In that case this would give a unique id for each item.

{% for blog_post in blog_posts.object_list %}
<div id="{{ forloop.counter }}" class="post-preview">
  <a href="{{ blog_post.get_absolute_url }}">
     <h2 class="post-title">
        {{ blog_post.title }}
     </h2>
  </a>
{% if settings.BLOG_USE_FEATURED_IMAGE and blog_post.featured_image %}
   {% block blog_post_list_post_featured_image %}
      <a href="{{ blog_post.get_absolute_url }}">
      <img class="img-thumbnail" src="{{ MEDIA_URL }}{% thumbnail blog_post.featured_image 730 474 %}">
      ...

Also, FYI: if there are 101 items in your queryset and you give id=1 for the first one and 101st one, id will not be unique. In HTML you should give unique ids to elements.

Upvotes: 0

Related Questions