简体   繁体   中英

How to pass multiple values from models.py to HTML via views.py in Django

I have a following models.py for my Django blog, I made a following views.py to pass the value of the slug for my URL parameter.
However I am struggling to create a model in views to get other data(person & description) from Category class.
I have tried some patterns by myself but can not pass them to HTML. (always Error or not showing)
Can you please give me some idea of how to solve this.

models.py

class Category(models.Model):
    person = models.CharField(max_length=20)
    description = models.TextField()
    slug = models.SlugField()
    def __str__(self):
        return self.person

views.py

def blog_category(request, category):
    posts = Post.objects.filter(categories__slug__contains=category).order_by("-created_on").distinct()
    context = {"category": category, "posts": posts}

    return render(request, "blog_category.html", context)

HTML(Localhost:8000/slug)

{{ person }}
{{ description }}

this is full code of my models.py

class Category(models.Model):
    person = models.CharField(max_length=20)
    description = models.TextField()
    slug = models.SlugField()
    def __str__(self):
        return self.person


class Recommender(models.Model):
    recommender_name = models.CharField(max_length=20)
    slug = models.SlugField()

    def __str__(self):
        return self.recommender_name


class Post(models.Model):
    book_title = models.CharField(max_length=255)
    author = models.CharField(max_length=255)
    book_link = models.CharField(max_length=255)
    recommenders = models.ForeignKey("Recommender", on_delete=models.CASCADE,)

    source = models.TextField()
    source_link = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    categories = models.ManyToManyField("Category", related_name="posts")

    slug = models.SlugField()

    def __str__(self):
        return self.book_title

I would look at this example .

Namely, if you render the template like it is shown in the example, you should be able to do

{{ category.person }} {{ category.description }}
posts = Post.objects.filter(categories__slug__contains=category).order_by("-created_on").distinct()

Is going to return a queryset. It can have more than one instance of the model class (since you are using filter ). In your context you are sending this queryset as posts to your templates.

So in your HTML you can use something like this. You need to use a for loop since there can be more than one item in posts .

{% for post in posts %}
    {% for category in post.categories.all %}
        {{ category.person }}
        {{ category.description }}
    {% endfor %}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM