'Django Template Queryset Issue
So Im literally going crazy trying to understand why I can't retrieve an individual value from a dictionary that's being passed to my template.
def createCharacterSkills(request):
user = request.user
if user.is_authenticated and request.method=="GET":
characterid = request.session["characterid"]
print(characterid)
characterrecord = character.objects.filter(pk=characterid)
print(characterrecord.values())
return render(request, "characters/createcharacter2.html", {'characterrecord':characterrecord})
Is what I am passing to my template. Below is the relevant code in my template:
<b>Name: </b>{{ characterrecord.values }} <br>
<b>Player Reference: </b>{{ characterrecord.id }}<br>
The characterrecord.values is working correctly and returning the whole dictionary as expected.
<QuerySet [{'id': 48, 'player_id': 1, 'character_name': 'Avalon', 'character_race': 'Dwarf', 'character_faction': 'Jhereg', 'character_group': 'Moredhel', 'ambidexterity': 0, 'dagger': 0, 'one_handed_weapon': 0, 'pole_arms': 0, 'projectile_weapons': 0, 'shield': 0, 'two_handed_weapons': 0, 'thrown_weapons': 0, 'wear_light_armour': 0, 'wear_medium_armour': 0, 'wear_heavy_armour': 0, 'wear_extra_heavy_armour': 0, 'body_development': 0, 'literacy': 0, 'surgeon': 0, 'numeracy': 0, 'alchemist': 0, 'crafting': 0, 'evaluate': 0, 'ranger_1': 0, 'ranger_2': 0, 'make_and_read_maps': 0, 'recongnise_forgery': 0, 'poison_lore_1': 0, 'posion_Lore_2': 0, 'potion_lore_1': 0, 'potion_lore_2': 0, 'ritual_contribute': 0, 'ritual_magic': 0, 'invocation': 0, 'corporeal_1': 0, 'corporeal_2': 0, 'mage_1': 0, 'mage_2': 0, 'shamanism_1': 0, 'shamanism_2': 0, 'forage': 0, 'meditation': 0, 'vet_ritual_magic': 0, 'vet_ritual_contribute': 0, 'chameleon': 0, 'fearless': 0, 'natural_armour': 0, 'sense_magic': 0, 'track': 0, 'intuition': 0, 'poison_resistance': 0, 'resist_magic': 0, 'sense_trap': 0, 'iron_will': 0, 'resist_disease': 0, 'discern_truth': 0, 'scrounge': 0, 'tricks_of_the_trade': 0, 'versatility': 0, 'regeneration': 0, 'extra_body_dev': 0}]>
However when I try to address and individual key in the dictionary I have no value in the template. I have done this numerous times in the past with no issue so I am at a loss as to why I am having this issue now.
Any help would be greatly appreciated.
Solution 1:[1]
You here have a QuerySet of dictionaries, so in that case you should enumerate over the QuerySet.
But here you filter on the primary key, so you can obtain a single item:
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
@login_required
def createCharacterSkills(request):
characterrecord = get_object_or_404(character, pk=request.session['characterid'])
return render(request, 'characters/createcharacter2.html', {'characterrecord': characterrecord})
there is no need to use .values(…) [Django-doc]: by using a model object, you do not erase the model layer.
Note: It is often better to use
get_object_or_404(…)[Django-doc], then to use.get(…)[Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, theget_object_or_404(…)will result in returning a HTTP 404 Not Found response, whereas using.get(…)will result in a HTTP 500 Server Error.
Note: You can limit views to a view to authenticated users with the
@login_requireddecorator [Django-doc].
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
