I got an error:
OSError at /app/
[Errno 63] File name too long: "/Users/xxx/testapp/templates/jinja2/{'items': [{'---': '---', 'A': 'a', 'B': 'b', 'C: 'c', 'D': 'd'}]}
I wanna embed json_dict
of json_parse method's result in views.py
to index.html
. I wrote in views.py
like
from django.shortcuts import render
import json
def index(request):
f = open('./data/data.json', 'r')
json_dict = json.load(f)
return render('./index.html',json_dict)
I wrote in index.html like
<html>
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css">
</head>
<body>
<select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
{% for k, v in json_dict.items %}
<option>{{ v }}</option>
{% endfor %}
</select>
</body>
</html>
json_dict
has dictionary of {'items': [{'---': '---' ~~~
.I really cannot understand why I cannot send json_dict
to index.html
. Am I wrong to use render method? How can I fix this?
urls.py of app(child app) has views.py&index.html is
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
urls.py of parent app is
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^app/', include('app.urls')),
]
Traceback is
Traceback:
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xxx/testapp/app/views.py" in index
8. return render('./index.html',json_dict)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
67. template = get_template(template_name, using=using)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py" in get_template
21. return engine.get_template(template_name)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/backends/django.py" in get_template
39. return Template(self.engine.get_template(template_name), self)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/engine.py" in get_template
162. template, origin = self.find_template(template_name)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/engine.py" in find_template
136. name, template_dirs=dirs, skip=skip,
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loaders/base.py" in get_template
38. contents = self.get_contents(origin)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loaders/filesystem.py" in get_contents
28. with io.open(origin.name, encoding=self.engine.file_charset) as fp:
Exception Type: OSError at /app/
Exception Value: [Errno 63] File name too long: "/Users/xxx/testapp/templates/jinja2/{'items': [{'---': '---', ‘A’: ‘a’, ‘B’: ‘b’, ‘C: ‘c’, ‘D’: ‘d’}]}
After editing by an answer,I got an error TypeError at /app/ init () got an unexpected keyword argument 'match_extension' . Traceback says Traceback:
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py" in getitem 65. return self._engines[alias]
During handling of the above exception ('django'), another exception occurred:
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xxx/testapp/app/views.py" in index 13. return render(request, 'jinja2/index.html', {'json_dict': json_dict})
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/shortcuts.py" in render 30. content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py" in render_to_string 67. template = get_template(template_name, using=using)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py" in get_template 18. engines = _engine_list(using)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py" in _engine_list 72. return engines.all() if using is None else [engines[using]]
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py" in all 89. return [self[alias] for alias in self]
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py" in 89. return [self[alias] for alias in self]
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py" in getitem 80. engine = engine_cls(params)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/template/backends/django.py" in init 32. self.engine = Engine(self.dirs, self.app_dirs, **options)
Exception Type: TypeError at /app/ Exception Value: init () got an unexpected keyword argument 'match_extension'
Your call to render()
is wrong, try this :
def index(request):
with open('./data/data.json', 'r') as f:
json_dict = json.loads(f.read())
return render(request, 'index.html', {'json_dict': json_dict})
Everything is in the doc :
https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.