How to update choices for forms.ChoiceField in Django?

20
October 22, 2019, at 9:10 PM

I have field in forms.py:

main_choices = ((1,'option 1'),(2,'option 2'),(3,'option 3'))
sub_choices = ((1,'PlaceHolder'))
class MainForm(forms.Form):
    type = forms.ChoiceField(choices=main_choices)
    sub_type = forms.ChoiceField(choices=sub_choices)

HTML:

<form action="{% url 'app:get_files' %}" method="POST">
  {% csrf_token %}
  {{ form.type }}
  {{ form.sub_type }}
  <button type="submit" class="app">GET</button>
</form>

view.py

def get_files(request):
    //Some action with complete form
    return FileResponse(zip_file, as_attachment=True)
def get_ajax(request):
    if request.is_ajax():
        data = request.POST.copy()
        if 'type' in data:
            type = int(data['type'])
            if type == 2:
                sub_choices = getChoices(type)
            elif type ==3:
                sub_choices = getChoices(type)
        return HttpResponse()

Currently, server catches ajax post data from the type field. I don't know how to put sub_choices from get_ajax action to sub_type field in forms. Can anyone explain how to do that?

READ ALSO
Why does my tkinter object keep changing shape?

Why does my tkinter object keep changing shape?

I have created an image on a canvas in tkinter that responds to a button eventAnd, the object is created on position x and position y where that event took place

36
Order coplanar points for drawing them

Order coplanar points for drawing them

I have an algorithm wich generates from three to six points of intersection between a plane and the edges of a cube which contains that planeFor drawing the plane (which gets drawn a polygon) OpenGL needs the vertex to be ordered as in the following picture:

28
Bytes input is INCORRECT (can&#39;t convert to JSON) in Python

Bytes input is INCORRECT (can't convert to JSON) in Python

Basically, I have this API end point that will be called if you make a POST request to itThe problem is for some reason, I can't convert the bytes to JSON so I can access the data

53
Python Email message not producing an output

Python Email message not producing an output

I have to design a class Message that models an e-mail messageA message has a recipient, a sender, and a message text

43