Jshee
Jshee

Reputation: 2686

Django Template is Printing Object Representation instead of Object Value

views.py

@login_required 
def ManageDomain(request): 
    AssocNotAuthDomains = Tld.objects.filter(FKtoUser_id=request.user,auth=0)
    AssocAuthDomains = Tld.objects.filter(FKtoUser_id=request.user,auth=1)

    return render(request, 'site/account/template.html', {
         'AssocNotAuthDomains':AssocNotAuthDomains,
         'AssocAuthDomains':AssocAuthDomains
    })

template.html

{% if AssocAuthDomains or AssocNotAuthDomains  %}
<div class="acctDomains">
    <h3 class="Titles"><img src="{% static "img/templated/acct/AuthDomainSm.png" %}" width="22" height="22" alt="Authorized Domain(s)" /> Verified Domains</h3>
    <ul>
    {% for authdomain in AssocAuthDomains %}
        <li>{{ authdomain }}<span><a class="scan" href="/Account/PerformScan/?d={{ authdomain }}">Scan now</span></a></li>
    {% endfor %}
    </ul>
    <h3 class="Titles"><img src="{% static "img/templated/acct/UnAuthDomain.png" %}" width="22" height="22" alt="Unverified Domain(s):" /> Unverified Domains</h3>
    <ul>
    {% for notauthdomain in AssocNotAuthDomains %}
        <li>{{ notauthdomain }}<span><a class="scan" href="Verify Now">Verify now</span></a></li>
    {% endfor %}
    </ul>
</div><!--acctDomains-->

This prints out:

Tld object (1)

Tld object (2)

instead of the value e.g. websiteabc.com

What am I doing wrong?

Thanks

Upvotes: 1

Views: 776

Answers (2)

Nafees Anwar
Nafees Anwar

Reputation: 6598

You have to implement __str__ in your model if you want to render a model instance like this

class Tld(models.Model):
    def __str__(self):
        # return something meaningful here like self.some_attribute

Or you have to explicitly use an attribute in template like this

 {{ authdomain.some_attribute }}

Otherwise python does not know how to get something meaningful out of an object to show.

Upvotes: 1

Razhel
Razhel

Reputation: 1

It's because you are trying to display the whole object, not the value.

You need to replace

{{ notauthdomain }}

with

{{ notauthdomain.name }} 

replace 'name' with the field name of your value - websiteabc.com

Upvotes: 0

Related Questions