0

I have ListView and select with options in my Django template and i want to get value from select in my listview to change response by this data but i got None when i try to print that

How am i suppose to do that?

views.py

    def get_queryset(self):
        url = config('REPORT_PROJECT_BUDGET')

        select_front = self.request.GET.get('selection')

        payload = {'ObjectGUID': select_front, }

        print(payload)
        # response = requests.get(url, auth=UNICA_AUTH, params=payload).json()

        # print(response)

        queryset = []

        return queryset

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['OBJECTS'] = ObjectList.objects.all()
        return context

template.html

<div class="select">
    <select id="g1" name="selection">
        {% for obj in OBJECTS %}
            <option value="{{ obj }}">{{ obj }}</option>
        {% endfor %}
        <option value="1">1</option>
    </select>
</div>
Kirill_N
  • 76
  • 8

2 Answers2

0

You forgot the name attribute to your select element, your template should be like this,

<select name='selection'>
    {% for obj in OBJECTS %}
    <option value="{{ obj.pk }}">{{ obj }}</option>
    {% endfor %}
    <option value="1">1</option>
</select>
Darsh Modi
  • 289
  • 1
  • 9
  • Yeah i forgot that but i already changed my question but this solution still has None – Kirill_N Mar 21 '23 at 07:23
  • you can do it like this select_front = self.request.GET.get('selection', 1) and make sure that you get the value in selection otherwise it will take 1 and remove the last option tag from the select element. – Darsh Modi Mar 21 '23 at 07:27
  • I got an json error when trying to put this params into my response requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) – Kirill_N Mar 21 '23 at 07:34
  • can you take a screenshot and show it here so I can check and guide you properly? – Darsh Modi Mar 21 '23 at 07:40
  • The main problem is that value from select not sending dynamically info into my get_queryset function and i dont know how to fix that – Kirill_N Mar 21 '23 at 07:56
  • check my answer I have updated it for you. – Darsh Modi Mar 23 '23 at 09:46
0

You can try

select_front = request.GET['selection']

'selection' is name of your dropdown select

Arik setyawan
  • 29
  • 1
  • 7
  • I got an error "django.utils.datastructures.MultiValueDictKeyError: 'selection' " when i use this method – Kirill_N Mar 21 '23 at 07:27
  • try this : https://stackoverflow.com/a/5895652/9903641 first check your 'selection' is in request.GET if your 'selection' is not exists, maybe your input isn't getting sent to Django – Arik setyawan Mar 21 '23 at 07:37