Fornow
Fornow

Reputation: 79

How to check the name of a model in a Django Template

I'm trying to get the name of a model in my template so i can give it a different design in the template

#views.py
class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html' 
    context_object_name = 'posts'
    paginate_by = 15

    def get_queryset(self):
        posts = []
        shared_post = []
        if self.request.user.is_authenticated:
            user_id = self.request.user.id
            view_user_post = Post.objects.filter(user=self.request.user)
            user_profile = User.objects.get(id=user_id).profile
            # print(user_profile)

            for profile in user_profile.follower.all():
                for post in Post.objects.filter(user=profile.user):
                    posts.append(post)

            for profile in user_profile.follower.all():
                for share in Share.objects.filter(user=profile.user):
                    shared_post.append(share)

            chain_qs = chain(posts, view_user_post, shared_post)
            print(chain_qs)
            return sorted(chain_qs, key=lambda x: x.date_posted, reverse=True)

        else:
            posts = Post.objects.all().order_by('?')
            return posts

#models.py
class Share(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    content = models.TextField(max_length=140, null=True, blank=True)
    date_posted = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return '{}- {}'.format(self.post.title, str(self.user.username))
class Post(models.Model):
    title = models.CharField(max_length=140)
    content = models.TextField(validators=[validate_is_profane])
    likes = models.ManyToManyField(User, related_name='likes', blank=True)
    date_posted = models.DateTimeField(default=timezone.now)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='post_pics', blank=True)
    image_2 = models.ImageField(upload_to='post_pics', blank=True)
    image_3 = models.ImageField(upload_to='post_pics', blank=True)
    restrict_comment = models.BooleanField(default=False)
    saved = models.ManyToManyField(User, related_name='saved_post', blank=True)

I need a way to check the name of the model in the template possibly an if/ else statement to check properly. thanks

Upvotes: 1

Views: 1225

Answers (2)

Ben Boyer
Ben Boyer

Reputation: 1234

What about create a function inside your model that will return the name of the model?

Inside your models.py for each model:

def get_my_model_name(self):
    return self._meta.model_name

Inside your template then yo can do something like:

{%if post.get_my_model_name == 'post'%}
    Do something ...

Upvotes: 1

ivissani
ivissani

Reputation: 2664

Instead of checking the model name I suggest you implement a boolean property in each model that returns True in one case and False in the other one. For example:

class Post(models.Model):
    # whatever fields and methods

    @property
    def is_shared(self):
        return False


class Share(models.Model):
    # whatever fields and methods

    @property
    def is_shared(self):
        return True

Then in your template just check {% if post.is_shared %}

Upvotes: 1

Related Questions