Home > front end >  Django Can I show one value in html without for loop?
Django Can I show one value in html without for loop?

Time:04-04

I want to show data from view in html. This is views.py

views.py

def addDeviceForm(request):

    key=request.GET['key']
    device=Device.objects.filter(key=key)
    data = {'device':device}
    return render(request,'add_form.html', data)

I try to show one data in html file like but it's not display value.

<b>{{ device.key }}</b>

If I show data with for like this code it have no problem.

{% for device in device %}
<b>{{ device.key }}</b>
{% endfor %}

I edit views.py like this.

    key="001"
    device=Device.objects.filter(key=key)
    data = {'device':device,'key':key }
    return render(request,'add_form.html', data)

And show in html like this.

{{ key.key }}

It still not show value.

Can I show one value in html without for loop?

CodePudding user response:

In template

# https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#first
<b>{{ data.first.key }}</b>

OR

If you want to make a filter which returns nth item of your object then:

  • In your app create a directory templatetags inside that directory create a file __init__.py and a file with any name filters.py

In filters.py

from django import template

register = template.Library()


@register.filter
def get_item(myList, index=0):
    """
    Django template filter to get nth item of list
    """
    if index < 0:
        return myList[0]
    else:
        return myList[index]

In template

The filter get_item can be used as: filters is the name of the file in templatetags

{% load filters %}
<!-- n will be item 0, 1, 2, ... -->
<b>{{ data|get_item:"n" }}

CodePudding user response:

def add_device_form(request):
    key=request.GET['key'] # you should probably use a FORM here 
    # device=Device.objects.filter(key=key) you don't need a queryset
    # and querysets are plural, they are a set of the rows, not a single row
    device = get_object_or_404(Device, key=key) # Throws 404 if obj doesn't exist
    context = {'device':device} 
    return render(request,'add_form.html', context)

CodePudding user response:

the filter method of a model class will always return a query set to you not an object if you want to return an object using the filter method you should use this:

device=Device.objects.filter(key=key).last()

or

device=Device.objects.filter(key=key).first()

this way you will get the last or first object of your queryset instead of itself.

CodePudding user response:

Yes, you can show one value in html, you must use get()django-doc or better get_object_or_404()django-doc.

views.py

from django.shortcuts import get_object_or_404, render


def addDeviceForm(request):

    key=request.GET['key']
    device=get_object_or_404(Device,any_key_name=key)
    data = {'device':device}
    return render(request,'add_form.html', data)

Then, in your template file you will get an object device in which you can access all its properties.

Template file

<b>{{ device.any_key_name }}</b>

Note: filter()djano-doc always returns new QuerySet containing objects that match the given lookup parameters as stated in docs. So, only use it with looping, since you don't know whether database has 1 record or more.

  • Related