I couldn't access the kwargs in the html template by {{ view.kwargs.foo }}. Not sure why, is it because something's different with DRF so I need a different syntax to access it?
My html template ('polls/character_description_list.html'):
{% block content %}
<table>
{% for description in descriptions %}
<tr>
<td><b>{{ description.title }}</b></td>
<td>{{ description.content }}</td>
</tr>
{% endfor %}
</table>
<form action="{% url 'polls:description_detail_create_from_character' %}">
<input type="hidden" value="{{ view.kwargs.character_id }}" name="character_id"> <!-- This is where I attempt to access the kwargs but can't get it, although I can attempt to output it anywhere else for debugging -->
<input type="submit" value="New Description"/>
</form>
{% endblock %}
Therefore when submitting I expect to go to:
http://localhost:8000/myapp/description_detail_create_from_character/?character_id=1
But in reality the id is missing:
http://localhost:8000/myapp/description_detail_create_from_character/?character_id=
To check if the character_id token I am looking for is in kwargs, I did try to breakpoint (using PyCharm) in get_serializer_context:
def get_serializer_context(self):
context = super(CharacterDescriptionListView, self).get_serializer_context()
return context
Examined the context, I can find 'view' -> kwargs -> 'character_id', with the value I am expecting, so it should work.
This is my views.py:
# Already has a chain of hierarchy but basically it descends from generics.ListCreateAPIView
class CharacterDescriptionListView(DescriptionViewMixin, CustomNovelListCreateView):
template_name = 'polls/character_description_list.html'
def get_filter_object(self):
return get_object_or_404(Character, id=self.kwargs['character_id'])
def get_queryset(self):
characterObj = self.get_filter_object()
return Description.objects.filter(character=characterObj)