{"id":1295,"date":"2022-08-30T15:15:20","date_gmt":"2022-08-30T15:15:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/why-am-i-getting-a-nameerror-when-trying-to-call-my-javascript-array-in-a-template-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:20","modified_gmt":"2022-08-30T15:15:20","slug":"why-am-i-getting-a-nameerror-when-trying-to-call-my-javascript-array-in-a-template-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-am-i-getting-a-nameerror-when-trying-to-call-my-javascript-array-in-a-template-collection-of-common-programming-errors\/","title":{"rendered":"Why am I getting a NameError when trying to call my Javascript array in a template?-Collection of common programming errors"},"content":{"rendered":"<p>Here is the view callable for my home page that defines jsdata:<\/p>\n<pre><code>@view_config(route_name='home_page', renderer='templates\/edit.pt')\ndef home_page(request):\n    if 'form.submitted' in request.params:\n        name= request.params['name']\n        input_file=request.POST['stl'].filename\n        vertices, normals = [],[]\n        for line in input_file:\n            ....\n\n        ordering=[]\n\n        ...parsing data...\n\n        data=[vertices,ordering]\n        jsdata=json.dumps(data)\n        renderer_dict = dict(name=name,data=jsdata)\n        ...\n        html_string = render('tutorial:templates\/view.pt', renderer_dict, request=request)\n        with open(filename,'w') as file:\n                file.write(html_string)\n        return HTTPFound(location=request.static_url('tutorial:pages\/%(pagename)s.html')% {'pagename':name}) \n\n    return {}    \n<\/code><\/pre>\n<p>I have imported json as well in that file (views.py). Here it is being rendered on the view.pt template.<\/p>\n<pre><code>\nvar data = ${structure:jsdata};\n\n<\/code><\/pre>\n<p>the NameError points to the jsdata part of that second line. Why is this happening? Doesn&#8217;t name error mean that it is undefined? Why is it saying that jsdata is undefined? Should it be passed as a keyword into the render function?<\/p>\n<p>Also: I am aware that there may be errors below that jsdata definition. I have included that part in case it helps you see what I want to be doing with jsdata<\/p>\n<ol>\n<li>\n<pre><code>renderer_dict = dict(name=name,data=jsdata)\n<\/code><\/pre>\n<p>You&#8217;re putting it in as <code>data<\/code> rather than <code>jsdata<\/code>. Therefore you would want <code>${structure:data}<\/code>, or to change the <code>renderer_dict<\/code> assignment.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:15:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here is the view callable for my home page that defines jsdata: @view_config(route_name=&#8217;home_page&#8217;, renderer=&#8217;templates\/edit.pt&#8217;) def home_page(request): if &#8216;form.submitted&#8217; in request.params: name= request.params[&#8216;name&#8217;] input_file=request.POST[&#8216;stl&#8217;].filename vertices, normals = [],[] for line in input_file: &#8230;. ordering=[] &#8230;parsing data&#8230; data=[vertices,ordering] jsdata=json.dumps(data) renderer_dict = dict(name=name,data=jsdata) &#8230; html_string = render(&#8216;tutorial:templates\/view.pt&#8217;, renderer_dict, request=request) with open(filename,&#8217;w&#8217;) as file: file.write(html_string) return HTTPFound(location=request.static_url(&#8216;tutorial:pages\/%(pagename)s.html&#8217;)% {&#8216;pagename&#8217;:name}) return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1295","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1295"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1295\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}