使用django的博客出错,使用HTML5的Jade不显示POST。

时间:2014-06-04 20:42:40

标签: python django html5 pug stylus

我有一个使用django,Jade和Stylus的简单博客。我创建的博客已经过测试,没有错误。

当我运行服务器代码执行时,在管理站点中添加了3个帖子作为示例,看看会是什么样子。我使用if语句来显示是否为空或显示帖子,遗憾的是显示没有帖子,问题是所有前端都是由jade和stylus编写的,我没有太多的知识在Jade写博客只是为了测试已经用过HTML5。 base.jade和home.html。我只是想知道为什么不在这里显示帖子就是代码。

model.py

from django.db import models from django.shortcuts import render_to_response, RequestContext from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

class Post(models.Model):
    title     = models.CharField(max_length=100, null=True, blank=True)
    content   = models.TextField()
    created   = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated   = models.DateField(auto_now=True)
    published = models.BooleanField(default=True)

    def __unicode__(self):
        return "%s" % self.title


def index(request):
    """ Home page """
    posts = Post.objects.filter(published=True).order_by('-created')[:2]

    paginator = Paginator(posts, 2)

    page_num = request.GET.get('page', 1)
    page = paginator.page(page_num)

    try:
        page = paginator.page(page_num)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)
    except PageNotAnInteger:
        page = paginator.page(1)

    return render_to_response('home.html', locals(), context_instance=RequestContext(request))

view.py

from vanilla import TemplateView


class HomePageView(TemplateView):
    template_name = "pages/home.html"

class PostPageView(TemplateView):
    template_name = "pages/post.html"

class PrivacyPageView(TemplateView):
    template_name = "careers/job_list.jade"

class AboutPageView(TemplateView):
    template_name = "pages/about.jade"

urls.py

from django.conf.urls import patterns, url

from .views import AboutPageView, HomePageView, PostPageView


urlpatterns = patterns('',
                       url(regex=r'^$', view=HomePageView.as_view(), name='home'),
                       url(regex=r'^post/(?P<id>\d+)/$', view=PostPageView.as_view(), name='post'),
                       url(regex=r'^about/$', view=AboutPageView.as_view(), name='about'),

home.html的

{% extends "base.jade"  %}
{% load i18n %}

{% block content %}
<h1>Welcome to JobPlus </h1><hr/>
{% if posts %}
        {% for post in page.objects_list %}
            <h3><a href="/templates/{{ post.id }}">{{ post.title }}</a></h3>
            <i>{{ post.created }}</i>
            <p>{{ post.content|safe|truncatewords:'100' }}</p>
            <hr/>
        {% endfor %}

        <ul class="pagination">

            {% if page.has_previous %}
                <li class="disabled"><a  href="?page={{ page.previous_page_number }}">&laquo;</a></li>
            {% endif %}

                <li><span>{{ page.number }}<span class="sr-only"></span></span></li>

            {% if page.has_next %}
                <li><a href="?page={{ page.next_page_number }}">&raquo;</a></li>
            {% endif %}

        </ul>
    {% else %}
         <p>There are no post! :(</p>
    {% endif %}
{% endblock %}

配置/ urls.py

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

admin.autodiscover()

urlpatterns = patterns('',
                       url(r'', include('apps.pages.urls')),
                       url(r'', include('apps.careers.urls')),
                       url(r'', include('apps.contact.urls')),
                       url(r'', include('apps.faq.urls')),
                       url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

base.jade

- load compressed i18n staticfiles
!!! 5
html(lang="en", data-ng-app)
    head
        meta(charset="utf-8")
        meta(http-equiv="X-UA-Compatible" content="IE=edge")
        title{% block title %}{% endblock %} | {{ site.name }}
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        meta(name="description", content="{% block description %}{% endblock %}")
        meta(name="robots", content="{% block robots %}{% endblock %}")
        meta(name="author", content="{% block author %}{% endblock %}")
        link(rel="stylesheet", href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css")
        link(rel="stylesheet", href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css")
        link(rel="shortcut icon", href="{% static 'images/favicon.ico' %}")
        - compressed_css 'style'
        block css
        body
            .navbar.navbar-inverse.navbar-static-top(role="navigation")
                .container-fluid
                    .navbar-header
                        button.navbar-toggle(type="button", data-toggle="collapse", data-target=".navbar-collapse")
                            span.sr-only Toggle Navigation
                            span.icon-bar
                            span.icon-bar
                            span.icon-bar
                        a.navbar-brand(href="#") JobPlus

                    .collapse.navbar-collapse
                        ul.nav.navbar-nav.pull-left
                            {% url 'home' as home %}
                            {% if request.path == home %}
                            li.active
                                a(href="{% url 'home' %}") {% blocktrans %}Home{% endblocktrans %}
                            {% else %}
                            li
                                a(href="{% url 'home' %}") {% blocktrans %}Home{% endblocktrans %}
                            {% endif %}
                            {% url 'about' as about %}
                            {% if request.path == about %}
                            li.active
                                a(href="{% url 'about' %}") {% blocktrans %}About us{% endblocktrans %}
                            {% else %}
                            li
                                a(href="{% url 'about' %}") {% blocktrans %}About us{% endblocktrans %}
                            {% endif %}
            .container
                block content
            hr
            .container
                .row-fluid
                    p &copy; &nbsp;
                        a(href="http://{{ site.domain }}") {{ site.name }}
                        &nbsp; {% now "Y" %}

任何想法,我错过了什么或者可能错了什么?

由于

0 个答案:

没有答案