0

On one of the pages of my django project I have a check box to select uuids corresponding to a model. This is what I have on the views.py page and it works well to create a checkbox.

def template_page(request, uuid_selects=None, option_1=False):
    ...
    class uuidCheckBox(forms.Form):
            uuid_selects =
            forms.ModelMultipleChoiceField(queryset=uuid_model, 
                                           widget=forms.CheckboxSelectMultiple)

    uuid_select_field = uuidCheckBox()
    args.update({'uuid_select_field': uuid_select_field})
    ...
    render(request, landing.html, args)

On my template page I have this:

<form action='get_uuid_selects' method='GET'>
   {{ uuid_select_field }}
   <input type='submit' value='Submit' />
 </form>

Which redirects back to this view:

def get_uuid_selects(request):
    uuid_selects = request.GET.getlist('uuid_selects')
    return landing_page(request, uuid_selects)

This all works fine however when I try to pass this list of uuids back to the template as a hidden value and return it when the user accesses another form the list doesn't work. For example I pass the list as an argument following the approach here This is the views.py for template:

def template_page(request, uuid_selects=None, option_1=False):
    ...
    if uuid_selects:
         args.update({'uuid_selects': json.dumps(uuid_selects)})
   ...
   render(request, landing.html, args)

Then I pass this list of uuids back to the template page so that it is a hidden value in another form on the template.html page.

      <form action='to_option_1' method='GET'>
        <button type='submit'>Option 1</button>
        {% if uuid_selects %}
          <input type="hidden" value= {{ uuid_selects }} name="uuid_selects">
        {% endif %}
      </form>

Then this is where the error surfaces once I've passed the list of uuids back to the views.py

def to_option_1(request):
    option_1 = True
    try:
        uuid_selects = request.GET.getlist('uuid_selects')
    except KeyError:
        uuid_selects = None
    return team_landing_page(request,
                             uuid_selects = uuid_selects,
                             option_1 = True)

The GET request only returns the first uuid (the list I tried was longer than 1) and in the wrong form to read as a uuid (this causes an error later on in the views.py but that's not relevant it's clear from the GET response that this is where the error occurs.:

['“["4322ac69-4c96-4fc1-b901-d68b5nnb0329",”

Clearly it has something to do with string formatting but I can't figure out how to make this work especially since passing the list of uuids works the first time when it just has to go from the HTML template back to the views.py - it's only once I repeat the process that things stop working.

Sorry if this is too much code just wanted to be very clear what the issue is.

GSam11
  • 101
  • 1
  • 1
  • 7
  • Can you show what is the rendered HTML for this part : `` please ? – Benbb96 Oct 04 '21 at 14:46
  • ["bb74eb3d-ca21-4015-94ae-123456789", "e7dca765-c13b-4503-97d8-123456789", "976758bb-c926-456b-bb88-8167817032c3"] - replaced these with fake UUIDs but it looks "correct when I render them on the page as just `{{ uuid_selects }}` so it's clearly something when I send it back to the views through the GET request – GSam11 Oct 04 '21 at 14:56
  • Did you try to wrap it with single quote like this : `` – Benbb96 Oct 04 '21 at 15:01

3 Answers3

1

Maybe the error is using getlist instead of just get ?

uuid_selects = request.GET.get('uuid_selects')
Benbb96
  • 1,973
  • 1
  • 12
  • 21
  • 1
    Thank you! It was a combination of this and a few other things (one of which you also outlined) I will put them in an answer combined. – GSam11 Oct 04 '21 at 15:12
0

You might need to change your function uuid_selects argument into:

def template_page(request, uuid_selects, option_1=False):
Ahmed Ablak
  • 718
  • 3
  • 14
  • Just removing the default None argument? Just tried this didn't seem to make any difference – GSam11 Oct 04 '21 at 14:49
  • Can you please add print statement and see what is the value of your `uuid_selects` inside `template_page` function – Ahmed Ablak Oct 04 '21 at 14:55
  • Looks fine the first time `["bb74eb3d-ca21-4015-94ae-123456789", "e7dca765-c13b-4503-97d8-123456789", "976758bb-c926-456b-bb88-8167817032c3"] ` but then looks like this the second time after I've passed it as a hidden value `['["bb74eb3d-ca21-4015-94ae-123456789",']` – GSam11 Oct 04 '21 at 15:00
  • Can you please try to print the value of `uuid_selects` inside your `template_page` as follows: `uuid_selects = {'uuid_selects': json.dumps(uuid_selects)}` and then `print(uuid_selects)` – Ahmed Ablak Oct 04 '21 at 15:07
0

The solution (thank you to Benbb96 for providing a few key tips) is to store the list of UUIDs as a JSON string back and forth. The input needs single quotes around it in the template.html page so that it is read in as a string.

<input type="hidden" value= '{{ uuid_selects }}' name="uuid_selects">

Then in the views.py function go_to_option_1 it should be read back in as a string not using the getlist function.

uuid_selects = request.GET('uuid_selects')

Finally to read the string back as a python list you need to parse the JSON string.

uuid_selects = json.loads(uuid_selects)
Nayana Chandran
  • 1,416
  • 1
  • 16
  • 30
GSam11
  • 101
  • 1
  • 1
  • 7