【问题标题】:Django template rendered partially (incorrectly)部分渲染的 Django 模板(不正确)
【发布时间】:2014-06-22 21:29:43
【问题描述】:

我正在使用带有 Neo4j 的 Django 作为 DB (nep4django)。

我有一个原始模板来显示 3 个城市的列表,就像我使用 Django python shell 一样:

In [8]: from mydb.models import Place

In [9]: cities = Place.objects.all()

In [10]: for city in cities:
   ....:     print city.name
   ....:     
Paris
Zurich
London

我的模板 city.html:

<!DOCTYPE html>
<html><head><title>Cities</title></head>
    <body>
        <h1>Cities</h1>
        <ul>
            {% for city in cities %}
            <li>{{ city.name }}</li>
            {% endfor %}
        </ul>
    </body></html>

在我的http://localhost:8000/cities/ 页面上,我没有收到任何错误,但唯一显示的是城市。所以,我显示了标题和 h1,但没有显示 ul 部分,我实际使用我的数据库。我该如何解决这个问题?

views.py 文件:

from django.shortcuts import render_to_response
from models import Place

def show_places(request):
   cities = Place.objects.all()
   return render_to_response('cities.html', {'List of cities': cities})

urls.py 文件:

from django.conf.urls import patterns, include, url
from neo4django import admin
from mydb.views import show_places

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    (r'^cities/', show_places),
)

【问题讨论】:

    标签: django django-templates neo4django


    【解决方案1】:

    List of cities 更改为cities。您正在分配该变量名称。

    【讨论】:

    • 有时我们在寻找复杂的解决方案,而问题并没有那么远。感谢您的版本,现在答案看起来更好了。你有很好的工具来变得伟大;)
    • 只是想说声谢谢,因为您的回答修复了我的错误(我忘记了上下文字典中的引号)。完全不相关,但谢谢:)
    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2010-12-08
    • 1970-01-01
    • 2016-06-07
    • 2017-10-19
    相关资源
    最近更新 更多