url和基于Django类的视图

2024-04-26 07:19:22 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在django(1.4)中使用基于类的视图的第一个项目,我在基于日期的归档文件不返回url时遇到了一些问题。我已经成功地在我的项目(企业内部网)中构建了几个不需要它们的应用程序,但网站的“新闻”部分确实需要基于日期的存档。在

年、月和日的存档看起来都很好,但我的个别文章没有产生我认为应该生成的网址。我很确定问题出在我的get_absolute_url函数中模型.py,因为如果我输入我希望他们直接让django找到并显示我想要的文章!在

从shell调用get_absolute_url函数得到:

NoReverseMatch: Reverse for 'news_detail' with arguments '('2013', 'Jan', '14', 'another-news-thang')' and keyword arguments '{}' not found.

我读了relevant docs和{a2}的具体参考,但我不太清楚我的错在哪里。在

我的模型.py是:

^{pr2}$

还有我的网址.py是:

from django.conf.urls import patterns, include, url
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView
from django.views.generic import ArchiveIndexView, YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView
from datetime import date
from news.models import News

sqs = SearchQuerySet().models(News)

urlpatterns = patterns('',
    url(r'^$', ArchiveIndexView.as_view(
        date_field = 'pub_date',
        model=News,
        context_object_name="latest_news",),
        ),
    url(r'^(?P<year>\d{4})/$', YearArchiveView.as_view(
        date_field = 'pub_date',
        model=News,
        context_object_name="year_archive",),
        ),
    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/$', MonthArchiveView.as_view(
        date_field = 'pub_date',
        model=News,
        context_object_name="month_archive",),
        ),
    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', DayArchiveView.as_view(
        date_field = 'pub_date',
        model=News,
        context_object_name="day_archive",),
        ),
    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', DateDetailView.as_view(
        date_field = 'pub_date',
        model=News,
        context_object_name="news_detail",),
        ),
    url(r'^search/$', SearchView(
        template='news/search.html',
        searchqueryset=sqs,
    ), name='haystack_search'),
)

很抱歉代码有点混乱;我倾向于在让它们正常工作后清理它们。非常感谢你的帮助。在


Tags: namefromimportviewurlfielddatemodel
1条回答
网友
1楼 · 发布于 2024-04-26 07:19:22

这里没有你的网址名称。在

如果没有在某处定义news_detail,则不能反转它。在

   url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',       
      DateDetailView.as_view(
        date_field = 'pub_date',
        model=News,
        context_object_name="news_detail",),
        ),
       name='news_detail'),  #< - you're missing this

相关问题 更多 >