Home > Net >  Passing a list of strings (uuids) back and forth between django views and template
Passing a list of strings (uuids) back and forth between django views and template

Time:10-05

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.

CodePudding user response:

Maybe the error is using getlist instead of just get ?

uuid_selects = request.GET.get('uuid_selects')

CodePudding user response:

You might need to change your function uuid_selects argument into:

def template_page(request, uuid_selects, option_1=False):

CodePudding user response:

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)
  • Related