Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't show specific model field in my HTML template

On my main page i want to show a category just like

{{ post.body }} but instead {{ post.body }} would be {{ post.category }}

here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{% extends "base.html" %}

{% block title %}Articles{% endblock title %}

{% block content %}
{% for post in post_list %}
  <div class="card">
    <div class="card-header">
      <span class="font-weight-bold"><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</a> |
      </span> &middot;
      <span class="text-muted">by {{ post.author }} |
      {{ post.created_at }}</span>
    </div>
    <div class="card-body">
      <!-- Changes start here! -->
      <p>{{ post.body }}</p>
      <p>{{ post.category }}</p>
      <a href="{% url 'post_edit' post.pk %}">Edit</a> |
      <a href="{% url 'post_delete' post.pk %}">Delete</a> |
      <a href="{% url 'category_list' %}">Category</a>
    </div>
    <div class="card-footer">
      {% for comment in post.comment_set.all %}
        <p>
          <span class="font-weight-bold">
            {{ comment.author }} &middot;
          </span>
          {{ comment }}
        </p>
      {% endfor %}
    </div>
    <!-- Changes end here! -->
  </div>
  <br />
{% endfor %}
{% endblock content %}

but cannot to figure out how. But cannot figure out how.

Here is my models

from django.conf import settings
from django.db import models
from django.urls import reverse

class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return (self.name)

    def get_absolute_url(self):
         return reverse("home")


class Post(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    category = models.ManyToManyField(Category, related_name='categories')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("post_detail", kwargs={"pk": self.pk})



class Comment(models.Model): 
    article = models.ForeignKey(Post, null=True, blank=True, on_delete=models.CASCADE)
    comment = models.CharField(max_length=140)
    author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,)

    def __str__(self):
        return self.comment

    def get_absolute_url(self):
        return reverse("post_list")

Thanks in advance

Added

{{ post.category }}

in here

{% extends "base.html" %}

{% block title %}Articles{% endblock title %}

{% block content %}
{% for post in post_list %}
  <div class="card">
    <div class="card-header">
      <span class="font-weight-bold"><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</a> |
      </span> &middot;
      <span class="text-muted">by {{ post.author }} |
      {{ post.created_at }}</span>
    </div>
    <div class="card-body">
      <!-- Changes start here! -->
      <p>{{ post.body }}</p>
      <p>{{ post.category }}</p>
      <a href="{% url 'post_edit' post.pk %}">Edit</a> |
      <a href="{% url 'post_delete' post.pk %}">Delete</a> |
      <a href="{% url 'category_list' %}">Category</a>
    </div>
    <div class="card-footer">
      {% for comment in post.comment_set.all %}
        <p>
          <span class="font-weight-bold">
            {{ comment.author }} &middot;
          </span>
          {{ comment }}
        </p>
      {% endfor %}
    </div>
    <!-- Changes end here! -->
  </div>
  <br />
{% endfor %}
{% endblock content %}

expected to see a category instead got post.category.None

>Solution :

To show the category in the HTML just like a property, you can add a property method in the model class and use it HTML, like this:

class Post(models.Model):
    ...
    
    @property
    def categories(self):
        return ', '.join([x.name for x in self.category.all()]

Then use it template like:

{{ post.categories }}

Alternatively as it is a ManyToMany field, you can iterate through it in the template and render the categories:

{{ for c in post.category }} {{ c.name }} {% if not forloop.last %}, {% endif %} {{ endfor }}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading