Search code examples
pythondjangoselectrow

django select complete single row and print


I want to select a single row completely to print it.

I mean to get name, address, city, state_province, country, website.

And also how I can select some of the column of onw row?

views.py:

from books.models import Publisher

def send(request):
    p = Publisher.objects.filter(id='29')
    return render(request, 'style.html', {'items': p})

style.html:

{{ items }}

models.py:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

def __str__(self):
    return self.name

Solution

  • This is how you access all the fields of a row in the template. You use the dot notation on the queryset object to access each field in your template (in your case, style.html):

    {{ items.name }}
    {{ items.address }}
    {{ items.city }}
    {{ items.state_province }}
    {{ items.country }}
    {{ items.website}}