show dynamic data from form value change in template

Question:

I’m new to django, i’m used to angular. i’m trying to do something that make sense to me in angular and I can’t achieve in django.

I’m working with python 3.9 and django 4.1

I simplified my case to this..

I have a form that I created and and a view for it, i have a select element, whenever i select something, i want to show what i selected.

so I created a LocationForm form class:

class LocationForm(forms.Form):
    apartment_type = forms.ModelChoiceField(queryset=ApartmentType.objects.all())

apartment type is just a list of apartment types (building, apartment, garden and so on)

i past the form to the view:

def location(request):
    context = {'form': LocationForm}
    return render(request, 'prospects/location.html', context)

and the code for the view:

{% load static %}

<form>
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save">
</form>

CCC{{ form.apartment_type.value }}DDD<br/>

the problem is that no matter what I select, form.apartment_type.value still shows None, it’s not being updated.

i guess i’m used to angular too much but this issue is really alarming for me if django only parses things statically, i guess i’m not used to it and i have no idea how to resolve it otherwise.

in my full code i want to reflect different type of items based of what chosen in the form but i can’t do that if nothing gets updated.

any ideas how to resolve this?

any information regarding this issue would be greatly appreciated, i’m really lost here.

#update

it looks like i wasn’t clear.

I want to understand the django template updates when variables are changed inside of it without refreshing the page.

only when i change the selection, without clicking save and sending the form i want to see the new value printed between CCC and DDD. currently i’m using {{ form.apartment_type.value }} but it stays None when i select an item.

if not.. how can i resolve this with django ?

i just said that i moved from angular and there it’s like that out of the box and if here it’s not, i hope there is a solution.

Asked By: ufk

||

Answers:

It seems that Django only compiles the pages when I render them first, if want to change anything in realtime I need to a javascript framework.

Found that at: https://stackoverflow.com/a/50189643/

Answered By: ufk