Django博客没有链接到详细视图

时间:2015-06-16 13:52:31

标签: django

我有一个我正在研究的django博客,我已经有了博客的第一部分,其中博客显示了数据库中的各种条目。然而,当我点击特定的博客文章时,它不会将我重定向到详细视图,它只是有点重定向到我可以看到所有博客帖子的博客:

通过在blog.html中点击此链接,我希望它链接到post.html中的特定帖子

<h2><a href="{% url 'entry_detail' slug=object.slug %}">{{ object.title }}</a></h2>

urls.py     来自django.conf.urls导入模式,url     来自。导入视图,Feed

urlpatterns = patterns(
    '',
    url(r'^feed/$', feed.LatestPosts(), name="feed"),
    url(r'^', views.BlogIndex.as_view(), name="blog"),
    url(r'^entry/(?P<slug>\S+)$', views.BlogDetail.as_view(), name="entry_detail"),
)

models.py

from django.db import models
from django.core.urlresolvers import reverse


class Tag(models.Model):
    slug = models.SlugField(max_length=200, unique=True)

    def __str__(self):
        return self.slug


class EntryQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)


class Entry(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200, null=True)
    body = models.TextField()
    slug = models.SlugField(max_length=200, unique=True)
    publish = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    tags = models.ManyToManyField(Tag)
    SHIRT_SIZES = (
        ('S', 'Small'),
        ('M', 'Medium'),
        ('L', 'Large'),
    )
    shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES, null=True, blank=True)
    objects = EntryQuerySet.as_manager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("entry_detail", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Blog Entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-created"]

views.py

from django.views import generic
from . import models


class BlogIndex(generic.ListView):
    queryset = models.Entry.objects.published()
    template_name = "blog.html"
    paginate_by = 3


class BlogDetail(generic.DetailView):
    model = models.Entry
    template_name = "post.html"

blog.html

{% include 'head.html' %}
{% include 'navbar.html' %}

<div class="container">

{% load django_markdown %}
<br>
<br>
{% for object in object_list %}
  <div class="post">
    <h2><a href="{% url 'entry_detail' slug=object.slug %}">{{ object.title }}</a></h2>

      <p class="meta"></p>

      <p class="meta">
          {{ object.created }} |
          Tagged under {{ object.tags.all|join:", " }}
      </p>
      {{ object.body|markdown }}
  </div>
{% endfor %}

    <hr class="featurette-divider">


{% if is_paginated %}
            <ul class="pager">
              <li {% if not page_obj.has_previous %}class="disabled"{% endif %}><a {% if page_obj.has_previous %}href="?page={{ page_obj.previous_page_number }}"{% endif %}>Prev</a></li>
              <li {% if not page_obj.has_next %}class="disabled"{% endif %}><a {% if page_obj.has_next %}href="?page={{ page_obj.next_page_number }}"{% endif %}>Next</a></li>
            </ul>
{% endif %}

    </div>
{% include 'footer.html' %}

post.html

{% load django_markdown %}

{% for object in object_list %}

  <div class="post">
    <h2><a href="{% url "entry_detail" slug=object.slug %}">{{ object.title }}</a></h2>

    <p class="meta">
      {{ object.created }} |
      Tagged under {{  object.tags.all|join:", " }} <p> Created by
      {{ object.author }} </p>
    </p>
    {{ object.body|markdown }}
  </div>

我还有另一个url.py,它将博客网址指向博客应用中的所有网址。不知道这是否有帮助:

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


urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'website.views.home', name='home'),
    url(r'^about/', 'website.views.about', name='about'),
    url(r'^contact/', 'contact.views.contact', name='contact'),
    url(r'^markdown/', include("django_markdown.urls")),
    url(r'^blog/', include('blog.urls')),
]

2 个答案:

答案 0 :(得分:3)

使用以下方法定义网址时:

url(r'^', views.BlogIndex.as_view(), name="blog"),
url(r'^entry/(?P<slug>\S+)$', views.BlogDetail.as_view(), name="entry_detail"),

第二个条目与/entry匹配,这不是您所期望的。我认为在你的情况下,你应该这样定义:

url(r'^$', views.BlogIndex.as_view(), name="blog"),
url(r'^entry/(?P<slug>\S+)$', views.BlogDetail.as_view(), name="entry_detail"),

^$将与/匹配,但不会与/entry/...

匹配

答案 1 :(得分:0)

感谢Brandon解决了问题。将我的网址格式从最具体到最不具体(最长到最短)重新排列。然后重新启动服务器。

urlpatterns = patterns(
    '',
    url(r'^entry/(?P<slug>\S+)$', views.BlogDetail.as_view(), name="entry_detail"),
    url(r'^feed/$', feed.LatestPosts(), name="feed"),
    url(r'^', views.BlogIndex.as_view(), name="blog"),    
)