Search code examples
djangoclassmodelsfield

Showing Django model field in HTML


I was wondering how to display a field that I have into an HTML output.

I have this class in my model.py

class Tower(models.Model):
    ap = models.IntegerField()

And I wanted to display the value ap holds into an HTML output and this is what I have so far in the html document.

{% for a in tower.tower_set.all %}
AP: {{a.ap}}<br>
{%endfor%}

Solution

  • I hope you also have a view like:

    from django.template.response import TemplateResponse
    from models import Tower
    
    def tower_list(request):
      return TemplateResponse(request, "your_template.html",
                              {"towers": tower.objects.all()})
    

    and your template "your_template.html" should look like this:

    {% for a in towers %}
      AP: {{a.ap}}<br>
    {%endfor%}