On Wednesday, 20 November 2019 03:47:24 UTC, Andrew Stringfield 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'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.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9eb7e5ae-7e2e-42a0-ab73-1206dad0166c%40googlegroups.com.

Reply via email to