Home > Software engineering >  How to check equal in template?
How to check equal in template?

Time:12-02

This is my model.

class Ad_company(models.Model):
    idx = models.AutoField(primary_key=True)
    subject = models.CharField(max_length=255)
    memo = models.CharField(max_length=255)
    content = models.TextField()
    is_display = models.CharField(max_length=1)
    writer = models.CharField(max_length=255)
    write_date = models.DateTimeField()
    update_date = models.DateTimeField()
    delete_date = models.DateTimeField()
    deadline_date = models.DateTimeField()
    reply = models.IntegerField(blank=True)
    hits = models.IntegerField(blank=True)
    ad_apply = models.IntegerField(blank=True)
    ad_category1 = models.CharField(max_length=255)
    ad_category2 = models.CharField(max_length=255)
    ad_place = models.CharField(max_length=255)
    ad_age = models.CharField(max_length=255)
    ad_sex = models.CharField(max_length=255)
    ad_budget = models.BigIntegerField()
    ad_length = models.CharField(max_length=255)
    is_done = models.CharField(max_length=1)
    is_pay = models.CharField(max_length=1)
    ad_service = models.CharField(max_length=255)
    ad_object = models.CharField(max_length=255)
    is_file = models.CharField(max_length=1)
    ad_require = models.CharField(max_length=255)
    contract_user = models.CharField(max_length=255, blank=True)

This is my view

def MyDashBoard(request):
    usertype = request.user.type
    if usertype == 1: #파트너스일때
        q = Q()

        q &= Q(ad_company_apply__username = request.user)
        q &= Q(ad_company_apply__is_done=1)
        queryset =  Ad_company.objects.filter(q).order_by('-idx')
        count_is_done = queryset.count()

        total_done_price = queryset.aggregate(score=Coalesce(Sum('ad_budget'), 0))

        q = Q()

        q &= Q(ad_company_apply__username = request.user)
        queryset =  Ad_company.objects.filter(q).order_by('-idx')
        count = queryset.count()
        page        = int(request.GET.get('p', 1))
        pagenator   = Paginator(queryset, 4)
        adlist      = pagenator.get_page(page)
        return render(request, 'account/setting/dashboard_partners.html',{"adlist":adlist, "count":count, "count_is_done":count_is_done, "total_done_price":total_done_price})

And This is my html template code

{% if adlist.contract_user == user %}
<span class="ad-state full-time">contract</span>
{% endif %}

adlist.contract_user is asdf and user is asdf same

but It doesn't work.

I think I need to change string or something?

I had this problem before. but I did by myself. but this issue is some different.

CodePudding user response:

Solved {% if adlist.contract_user|stringformat:"i" == request.user|stringformat:"i" %}

CodePudding user response:

The correct mode is checking the user primary key:


class Ad_company(models.Model):
    contract_user = models.ForeingKey(User)
{% if adlist.contract_user.id == request.user.id %}

However, if in your models contract_user saves the username, you can compare it to the username field request.user.username:

{% if adlist.contract_user == request.user.username %}
  • Related