Home > Mobile >  Can't show specific model field in my HTML template
Can't show specific model field in my HTML template

Time:01-06

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

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

here.

{% extends "base.html" %}

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

{% block content %}
{% for post in post_list %}
  <div >
    <div >
      <span ><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</a> |
      </span> &middot;
      <span >by {{ post.author }} |
      {{ post.created_at }}</span>
    </div>
    <div >
      <!-- 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 >
      {% for comment in post.comment_set.all %}
        <p>
          <span >
            {{ 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 >
    <div >
      <span ><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</a> |
      </span> &middot;
      <span >by {{ post.author }} |
      {{ post.created_at }}</span>
    </div>
    <div >
      <!-- 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 >
      {% for comment in post.comment_set.all %}
        <p>
          <span >
            {{ 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

CodePudding user response:

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 }}
  • Related