我收到了一个错误:
/ app /
中的OSError[Errno 63]文件名太长:“/ Users / xxx / testapp / templates / jinja2 / {'items':[{'---':'---','A':'a' ,'B':'b','C:'c','D':'d'}]}
我想在json_dict
中将views.py
json_parse方法的结果嵌入index.html
。我在views.py
写了
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)
我在index.html中写过
<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
包含{'items': [{'---': '---' ~~~
字典。我真的无法理解为什么我无法将json_dict
发送给index.html
。我使用render方法错了吗?我该如何解决这个问题?
app(子应用)的urls.py有views.py&amp; index.html是
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
父app的urls.py是
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:
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’}]}
通过回答编辑后,我在/ app /收到错误TypeError init ()得到了一个意外的关键字参数'match_extension'。 Traceback说 回溯:
getitem 中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py” 65. return self._engines [别名]
在处理上述异常('django')期间,发生了另一个异常:
内部文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/exception.py” 41. response = get_response(request)
_get_response中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/base.py” 187. response = self.process_exception_by_middleware(e,request)
_get_response中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/core/handlers/base.py” 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
索引中的文件“/Users/xxx/testapp/app/views.py” 13. return render(request,'jinja2 / index.html',{'json_dict':json_dict})
渲染文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/shortcuts.py” 30. content = loader.render_to_string(template_name,context,request,using = using)
render_to_string中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py” 67. template = get_template(template_name,using = using)
<_> get_template中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py” 18. engines = _engine_list(使用) _engine_list中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/loader.py” 72.如果使用的是其他[engine [using]]
,则返回engines.all()文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py” 89.返回[self [alias] for alias in self]
文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py”in 89.返回[self [alias] for alias in self]
getitem 中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/utils.py” 80. engine = engine_cls(params)
init 中的文件“/Users/xxx/myenv/lib/python3.5/site-packages/django/template/backends/django.py” 32. self.engine = Engine(self.dirs,self.app_dirs,** options)
异常类型:/ app /的TypeError 异常值: init ()得到意外的关键字参数'match_extension'
答案 0 :(得分:1)
您对render()
的来电是错误的,请尝试:
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})
一切都在文档中:
https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render