将其他Django Apps模型导入View - 应该是基本的

时间:2011-02-18 01:55:58

标签: django django-models django-templates

我意识到我在这里做了一些基本的错误,只是不确定它是什么。我没有收到任何错误,但是在加载页面时我没有显示任何模型数据。

这是我正在尝试做的事情: 应用:基础,博客,简历

我正试图从博客和简历中获取模型以显示在基础视图中。博客和简历应用程序都可以自行运行。

碱/ views.py

from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from testpro.blog.models import Post
from testpro.resume.models import Project

def main(request):
    """Main listing."""
    posts = Post.objects.all().order_by("-created")
    projects = Project.objects.all().order_by("-created")
    return render_to_response("list.html", dict(posts=posts, projects=projects, user=request.user))

list.html模板

{% extends "bbase.html" %}

{% block content %}
    <div class="main">
        <h3>Blog Posts</h3>
        <!-- Posts  -->
        <ul>
            {% for post in posts.object_list %}
                <div class="title">{{ post.title }}</div>
                <ul>
                    <div class="time">{{ post.created }}</div>
                    <div class="body">{{ post.body|linebreaks }}</div>
                </ul>
            {% endfor %}
        </ul>

        <!-- Projects  --> 
        <h3>Projects</h3>       
        <ul>
            {% for project in projects.object_list %}
                <div class="title">{{ project.title }}</div>
                <ul>
                    <div class="industry">{{ project.industry }}</div>
                    <div class="time">{{ project.created }}</div>
                    <div class="body">{{ project.body|linebreaks }}</div>
                </ul>
            {% endfor %}
        </ul>

    </div>

{% endblock %}

最后,urls.py

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^main/', 'base.views.main'),
    (r'^admin/', include(admin.site.urls)),
)

我犯了什么愚蠢的错误?模板呈现,它只是不包含任何模型数据。

修改:添加了bbase.html模板

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <title>{% block title %}Test Project{% endblock %}</title> </head>

<body>
    <div id="sidebar"> {% block sidebar %} {% endblock %} </div>
    <div id="container">
        <div id="menu">
            {% block nav-global %}

                <!-- MENU -->
                <h3>MyBlog</h3>
                {% if user.is_staff %}
                <a href="{% url admin:index %}">Admin</a>
                <a href="{% url admin:blog_post_add %}">Add post</a>
                {% endif %}

            {% endblock %}
        </div>

        <div id="content">
            {% block content %}{% endblock %}
        </div>
    </div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

{% for project in projects.object_list %}
# should be
{% for project in projects %}

{% for post in posts.object_list %}
# should be
{% for post in posts %}

QuerySet没有我知道的object_list属性,因此模板引擎无声地失败。