0

我正在使用 Django 1.5.1,现在我有一个网页显示我的数据库中的多个条目,但现在我想自己为每个条目创建一个页面。所以基本上当用户点击标题时,它会将他们带到一个单独的页面,只显示他们点击的条目。

我收到以下错误:

NameError at /
name 'Post' is not defined

以下是我认为与解决此问题相关的每个页面的代码。我还创建了一个 post.html 页面,因此不会是我的错误,views.py 和 urls.py 存储在 Django 的默认目录中,我的 post.html 和 index.html 存储在 /mysite/mysite/模板/博客/

索引.html

 {% extends 'base.html' %}
{% block title %} Roadcase WebApp {% endblock %}
{% block content %}
    <h1>Roadcase WebApp</h1>
    <p>Today's Date: {% now "F j, Y" %}</p>
    <div class="post">
        <table class="main">
            <tr class="title_head">
                <td><a href="">ID</a></td>
                <td><a href="">Roadcase</a></td>
                <td><a href="">User</a></td>
                <td><a href="">Location</a></td>
                <td><a href="">Date Taken</a></td>
                <td><a href="">Return Date</a></td>
                <td><a href="">Returned</a></td>
            </tr>
    {% for post in posts %}
            <tr>
                <td><a class="n_link" href="/blog/{{post.id}} ">{{post.id}}</a></td>
                <td>{{post.Roadcase_Number}}</td>
                <td>{{post.First_Name}} {{post.Last_Name}}</td>
                <td>{{post.Where_Did_It_Go}}</td>
                <td>{{post.Date_Taken|date}}</td>
                <td>{{post.Date_Returned|date}}</td>
                <td>{% if post.Returned == True %}
                        Yes
                    {% else %}
                        No
                    {% endif %}</td>
            </tr>
    {% endfor %}
    </table>
    </div>
{% endblock %}

视图.py

    from django.shortcuts import render, get_object_or_404
from blog.models import Post

#def index(request):
    # get the blog posts that are published
#   posts = Post.objects.filter(published=True)

#   # Order the content by descending id, (3 2 1)
#   posts = Post.objects.order_by('-id')
    # now return the rendered template
#   return render(request,'blog/index.html', {'posts': posts})

#def post(request, slug):
    # get the Post object
#   post = get_object_or_404(Post, slug=slug)
    # now return the rendered template
#   return render(request, 'blog/post.html', {'post': post})

class Index(ListView):
    model = Post
    template_name = 'blog/index.html'
    def get_queryset(self):
        return super(Index, self).get_queryset().filter(published=True).order_by('-id')

def PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post.html'

网址.py

    from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import DetailView

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$','blog.views.index'),
    url(r'^(?P<slug>[\w\-]+)/$','blog.views.post'),

    url(r'^(?P<pk>\d+)$', DetailView.as_view(
                            model=Post,
                            template_name="post.html")),
)

post.html

{% extends 'base.html' %}


{% block title %} Roadcase WebApp {% endblock %}
{% block content %}
    <h1>Roadcase WebApp</h1>
    <p>Today's Date: {% now "F j, Y" %}</p>
    <div class="post">
        <table class="main">
            <tr class="title_head">
                <td><a href="">ID</a></td>
                <td><a href="">Roadcase</a></td>
                <td><a href="">User</a></td>
                <td><a href="">Location</a></td>
                <td><a href="">Date Taken</a></td>
                <td><a href="">Return Date</a></td>
                <td><a href="">Returned</a></td>
            </tr>
            <tr>
                <td><a class="n_link" href="/blog/{{post.id}} ">{{post.id}}</a></td>
                <td>{{post.Roadcase_Number}}</td>
                <td>{{post.First_Name}} {{post.Last_Name}}</td>
                <td>{{post.Where_Did_It_Go}}</td>
                <td>{{post.Date_Taken|date}}</td>
                <td>{{post.Date_Returned|date}}</td>
                <td>{% if post.Returned == True %}
                        Yes
                    {% else %}
                        No
                    {% endif %}</td>
            </tr>
    </table>
    </div>
{% endblock %}
4

2 回答 2

0

我会这样做:

在你的views.py中:

from django.shortcuts import render, get_object_or_404
from django.views.generic import DetailView, ListView
from blog.models import Post

class Index(ListView):
    model = Post
    template_name = 'blog/index.html'
    def get_queryset(self)
        return super(Index, self).get_queryset().filter(published=True).order_by('-id')

def PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post.html'

在您的项目 urls.py 中:

from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/',include("blog.urls")),

)

在您的博客应用程序 urls.py 中:

from blog.views import PostDetailView, Index

urlpatterns = patterns('',
    url(r'^$',Index.as_view()),
    url(r'^post/(?P<slug>[\w-]+)/$',PostDetailView.as_view()),

    url(r'^post/(?P<pk>\d+)$', PostDetailView.as_view()),
)

编辑 添加进口

于 2013-07-05T14:31:36.703 回答
0

您在 urls.py 中有错误需要添加导入帖子,导致 DetailView 使用它作为模型

from blog.models import Post
于 2013-07-05T14:05:25.813 回答