我使用的是MongoDB和Djongo。 我已经使用mongorestore导入了一个现有的MongoDB,我可以在Django模板中列出所有数据库内容。
我想做什么:链接到列表中显示的每个文档的详细信息视图。
我已按照this post中的建议尝试打印MongoDB _id值,但得到错误:
字符串索引必须是整数
这些是我的文件:
模板 elenco_strutture.html
{% load strutture_ricettive_tags %}
{% block content %}
{% for struttura in strutture %}
<div class="post">
<div class="date">
{{ struttura.loc }}
</div>
<h1>Mongo ID: {{ object|mongo_id }}</h1>
<br>coord: {{ struttura.location|linebreaksbr }}
</div>
{% endfor %}
{% endblock %}
strutture_ricettive_tags.py
from django import template
register = template.Library()
@register.filter("mongo_id")
def mongo_id(value):
return str(value['_id'])
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.elenco_strutture, name='elenco_strutture'),
url(r'^struttura/(?P<mongo_id>[0-9]+)/$', views.struttura_detail, name='struttura_detail'),
]
views.py
from django.shortcuts import render, get_object_or_404
# Create your views here.
from .models import Strutture
def elenco_strutture(request):
strutture = Strutture.objects.all()
return render(request, 'strutture_ricettive/elenco_strutture.html', {'strutture': strutture})
def struttura_detail(request, _id):
struttura = get_object_or_404(Struttura, mongo_id=mongo_id)
return render(request, 'strutture_ricettive/struttura_detail.html', {'struttura': struttura})
回溯:
Environment:
Request Method: GET
Request URL: http://alberghi-sicilia.vmware/
Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'strutture_ricettive']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /var/www/django_virtualenv/progetti_django/alberghi_sicilia/strutture_ricettive/templates/strutture_ricettive/base.html, error at line 8
string indices must be integers
1 : {% load staticfiles %}
2 : <!doctype html>
3 : <html>
4 : <head>
5 : <meta charset="utf-8">
6 : <title>Strutture Ricettive Sicilia</title>
7 : <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
8 : <link rel="stylesheet" href="//maxc dn.bootstrapcdn.com/b ootstrap/3.2.0/css/bootstrap-theme.min.css">
9 : <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" "type=text/css">
10 : <link rel="stylesheet" href="{% static 'css/blog.css' %}">
11 : </head>
12 : <body>
13 : <div class="page-header">
14 : <h1><a href="/">home</a></h1>
15 : </div>
16 :
17 : <div class="content container">
18 : <div class="row">
Traceback:
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/django_virtualenv/progetti_django/alberghi_sicilia/strutture_ricettive/views.py" in elenco_strutture
15. return render(request, 'strutture_ricettive/elenco_strutture.html', {'strutture': strutture})
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
62. return template.render(context, request)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/backends/django.py" in render
61. return self.template.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render
175. return self._render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/loader_tags.py" in render
155. return compiled_parent._render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/loader_tags.py" in render
67. result = block.nodelist.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/defaulttags.py" in render
211. nodelist.append(node.render_annotated(context))
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in render
993. output = self.filter_expression.resolve(context)
File "/var/www/django_virtualenv/progetti_django/lib/python3.6/site-packages/django/template/base.py" in resolve
703. new_obj = func(obj, *arg_vals)
File "/var/www/django_virtualenv/progetti_django/alberghi_sicilia/strutture_ricettive/templatetags/strutture_ricettive_tags.py" in mongo_id
6. return str(value['_id'])
Exception Type: TypeError at /
Exception Value: string indices must be integers