Directly Accessing Dictionary Values

26 views
Skip to first unread message

Andrew Stringfield

unread,
Nov 19, 2019, 10:47:24 PM11/19/19
to Django users
Hello all,

     I am trying to access Dictionary values directly.  Here is my view:

def unique_key_query(request, unique_key):
        unique_key_object = simpleformmodel.objects.all().filter(id=unique_key)
        context = {'unique_key_object': unique_key_object}
        return render(request, "bash_file_page.html", context)

Here is my template:
{% if unique_key_object %}
        {% for question in unique_key_object %}
                <p>{{ question.filename }}</p>
        {% endfor %}
{% else %}
        <p>No data is available.</p>
{% endif %}

I can access Dictionary values with a for loop, but I just do not want to do that.  How can I access the values directly?

Thank you.

Suraj Thapa FC

unread,
Nov 19, 2019, 10:59:07 PM11/19/19
to django...@googlegroups.com
Return the unique key object... Like
return render(request, 'abc.html',unique_key)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b8ee9cc4-1d49-4464-b181-583042182576%40googlegroups.com.

Andrew Stringfield

unread,
Nov 20, 2019, 5:26:55 AM11/20/19
to Django users
First, thank you for the reply Suraj!  I think that I misstated what I was really trying to do.  I am trying to access the dictionary values directly in the Template and not in the View.  I am trying to remove:

        {% for question in unique_key_object %}
               <p>{{ question.filename }}</p>
       {% endfor %}

and just have:
<p>{{ unique_key_object.filename }}</p>




On Tuesday, November 19, 2019 at 10:59:07 PM UTC-5, Suraj Thapa FC wrote:
Return the unique key object... Like
return render(request, 'abc.html',unique_key)

On Wed, 20 Nov 2019, 9:18 am Andrew Stringfield, <lone...@gmail.com> wrote:
Hello all,

     I am trying to access Dictionary values directly.  Here is my view:

def unique_key_query(request, unique_key):
        unique_key_object = simpleformmodel.objects.all().filter(id=unique_key)
        context = {'unique_key_object': unique_key_object}
        return render(request, "bash_file_page.html", context)

Here is my template:
{% if unique_key_object %}
        {% for question in unique_key_object %}
                <p>{{ question.filename }}</p>
        {% endfor %}
{% else %}
        <p>No data is available.</p>
{% endif %}

I can access Dictionary values with a for loop, but I just do not want to do that.  How can I access the values directly?

Thank you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Suraj Thapa FC

unread,
Nov 20, 2019, 8:51:46 AM11/20/19
to django...@googlegroups.com
You can't 

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5dc09b06-1daa-432f-b31d-18a698bb0b4b%40googlegroups.com.

Andrew Stringfield

unread,
Nov 20, 2019, 8:57:29 AM11/20/19
to Django users
Okie-dokie. Thank you.

Jim Illback

unread,
Nov 20, 2019, 1:13:50 PM11/20/19
to Django users
Have you tried <list_field_name.0> for the first element, and so forth? Of course, you have to keep track of the number of entries.


> On Nov 20, 2019, at 5:57 AM, Andrew Stringfield <lone...@gmail.com> wrote:
>
> Okie-dokie. Thank you.
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83187d02-2f28-4652-8986-83dfcd65108e%40googlegroups.com.

Andrew Stringfield

unread,
Nov 20, 2019, 1:25:18 PM11/20/19
to Django users
I have not! I did see something about that on a website. Would it go something like: dictionary_name.fieldname.0 ? I will try it out later today.

Jim Illback

unread,
Nov 21, 2019, 11:18:45 AM11/21/19
to Django users
Check out this URL and the embedded Django link in the answers: https://stackoverflow.com/questions/1700661/how-to-access-array-elements-in-a-django-template.


On Nov 20, 2019, at 10:25 AM, Andrew Stringfield <lone...@gmail.com> wrote:

I have not!  I did see something about that on a website.  Would it go something like: dictionary_name.fieldname.0 ?  I will try it out later today.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Daniel Roseman

unread,
Nov 22, 2019, 4:03:39 AM11/22/19
to Django users
You've got some unfortunately bad answers here. But your question itself is very confusing, so it's not surprising.

You *don't have a dictionary here*. It's not clear why you think you do. You have a queryset, consisting of one or more model instances.

However, I *think* you are trying to ask why you have a queryset in the first place. And the answer is that that's what `filter` always gives you, no matter how many items it matches. But in your case you're querying by unique ID anyway, so you should only get one item. So, in a case like this, you should use `get`:

     unique_key_object = simpleformmodel.objects.get(id=unique_key)

and now in the template {{ unique_key_object.filename }} will work correctly.

-- 
DR.

Sencer Hamarat

unread,
Nov 22, 2019, 7:08:05 AM11/22/19
to django...@googlegroups.com
Hello Andrew,

If you sure you have only one object, or the first item is enough to do something in template; You can access value by calling object with index number of the object by {% unique_key_object.0.filename %} usage.
If there is no object in unique_key_object, template engine might fail silently.

If you really sure there is only one object will return on each execution of line below

        unique_key_object = simpleformmodel.objects.all().filter(id=unique_key)

Consider changing that line with this:

        unique_key_object = simpleformmodel.objects.get(id=unique_key)

This will fetch only one object if available. If not, it will throw exception.

On the toher hand, there are .first() and .last() methods are available with query.

If you convert that query to this:

        unique_key_object = simpleformmodel.objects.filter(id=unique_key).first()
or
        unique_key_object = simpleformmodel.objects.filter(id=unique_key).last()

You can get only one object like as querying with .get(), but lack of object is return None instead throwing exception.
BTW, you don't need to use .all() if you have to use .filter() or other query methods.



Regards,
Sencer HAMARAT



Reply all
Reply to author
Forward
0 new messages