0

Im building my first Django app. I want to call a javascript function that receives a list of strings. In my current version Im sending a Django object (called provider) property like this and it´s working fine:

<a href="#" class="button secondary tiny" onclick="download_provider_files('{{ provider.full_name }}')"><i class="fi-download"></i></a>

But now I want to call a Provider model method that returns a list of strings, so then I can do something with this list in Js method

def get_provider_files_urls(self):
        provider_files = self.provider_files.all()
        file_list = []
        for f in provider_files:
            file_list.append(f.file.name)
        return file_list

I tried this:

<a href="#" class="button secondary tiny" onclick="download_provider_files('{{ provider.get_provider_files_urls }}')"><i class="fi-download"></i></a>
              

and got this error:

Uncaught SyntaxError: missing ) after argument list

1 Answer 1

1

A quick and dirty way to generate a JS array from a list is to use the join filter

onclick="download_provider_files(['{{ provider.get_provider_files_urls|join:"', '" }}'])"

Probably cleaner to use the json_script filter though

Sign up to request clarification or add additional context in comments.

1 Comment

I have to adapt it a little bit but now is working. Thanks!! I will take a look also at json_script <a href="#" class="button secondary tiny" onclick="download_provider_files(['{{ provider.get_provider_files_urls|join:', ' }}'])"><i class="fi-download"></i></a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.