0

My model lop is contains a list of programs which I use for varying purposes. I want to use the name field as an argument for a javascript function.

I modified some of my lops so the modified versions have a "ver2" at the end of its name. What the Javascript function does is that it checks for the suffix "ver2" of the program. The javascript was originally found from here.

I read some similar questions and one of them said that I need to serialize the object

EDIT: Expanded view of views.py, Javascript console started working and now is included.

In my views.py (UPDATED)

  from django.core import serializers
           .      
           .
           .
           .
  def loppage(request):

  jsondata = serializers.serialize('json', lop.objects.all(),fields=('name'));

  ## get programs
  data = []
  types = Type.objects.all()
  for type in types:
  data.append([type.title, type.script_set.all()])
  context = {'lop': Lop.objects.all(), 'cat': data, 'jsondata':jsondata}

  ## render list
  return render(request, 'loppage.html', context)

In my templates file: Javascript/HTML (loppage.html):

<script>
function endsWithsuffix(progname, suffix) {
return progname.indexOf(suffix, progname.length - suffix.length) !== -1;} 

  </script> 

        .
        .
        .
        .
   {% for lop in type %}
      <p id="Options"><i>{{lop.options}}</i></p>
      <p id="Id"><a href="/ne/{{lop.id}}/">{{lop.name}}</a></p>

   <script type="text/javascript">
     if  (endsWithsuffix({{jsondata}}, 'ver2'))   {    //This I've tried with and without quotation marks, and with lop.name with and without quotation marks
         document.getElementById('Options').style.visibility = 'visible';
         document.getElementById('Id').style.visibility = 'visible';
    }

     else {
         document.getElementById('Options').style.visibility = 'hidden';
         document.getElementById('Id').style.visibility = 'hidden';
       }
    </script>
   {% endfor %}

But for whatever reason, the script doesn't seem to load (it loads as if the script wasn't even though).

As wardk suggested, I have now included my Javascript console which can be seen here

SyntaxError: invalid property id loppage:56:28

It's a long repetition of this same error on the same line as shown below

Debugger console highlights

if  (endsWithsuffix([{&quot;pk&quot;: 2, &quot;model&quot;: &quot;programs.lop&quot;,

I've been working on this way longer than I should but I can't get anywhere with it. Help.

2
  • You're missing the closing {% endfor %} before the opening <script> tag Commented Feb 19, 2015 at 22:06
  • @Brandon. Sorry, typo on my part (I didn't copy long enough but it's there in the original code). Fixed the question to match the correction. Commented Feb 20, 2015 at 5:21

1 Answer 1

1

You're applying endsWithSuffix on a json representation of lop.objects.all(). Shouldn't you test endsWithSuffix for {{lop.name}} instead?

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

14 Comments

I initially tried it with {{lop.name}} but it was mentioned in a similar question that I would need to convert it into json before I could use it to javascript. Still, I tried using {{lop.name}} with the json variable instead and it still doesn't seem to be running.
^sorry, correction: I tried again with {{lop.name}} while keeping the json variable somewhere else but no dice.
i think you need quotes around {{lop_name}}.
Tried that too, both with single and double quotes. Still not working :(
what appears in the page source exactly? did you check the javascript console? (developer.chrome.com/devtools/docs/console in chrome, something similar exists for firefox)
|

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.