Django 1.5为pk>提供404s 999

时间:2015-03-11 18:00:41

标签: python django

我正在尝试做什么 我有一个简单的视图,它将pk作为参数并执行操作。这个工作正常,直到pks大于999.现在他们返回404s。我正在努力解决这个问题。

我尝试了什么 我的观点如下:

def request_publication(request, pk):
    ...
    article = News.all_news.get(pk=pk) # all_news is a manager including unpublished articles
    article.status = article.HIDDEN_STATUS
    article.save()
    ...

网址正则表达式是这样的:

regex=r'^request-publication/(?P<pk>\d+)/',

我也试过了:

regex=r'^request-publication/(?P<pk>\d{4})/',

这使得它在pks上失败&lt;如预期的那样1000,但仍不适用于pk&gt; 999。

完整的urls.py是:

# core Django imports
from django.conf.urls import patterns, include, url

# local imports
from .models import News, Category, Attachment
from .views import (
    NewsHomeView,
    CategoryHomeView,
    NewsDetailView,
    NewsYearArchiveView,
    NewsMonthArchiveView,
    NewsDayArchiveView,
    NewsListView,
    NewsCreateView,
    NewsUpdateView,
    publish,
    request_publication,
)

urlpatterns = patterns('',
    url(
        regex = r'^$',
        view = NewsHomeView.as_view(),
        name = "news_home",
        ),
    url(
        regex = r'^add/$',
        view = NewsCreateView.as_view(),
        name = 'news_add',
    ),
    url(
        regex = r'^update/(?P<pk>\d+)/$',
        view = NewsUpdateView.as_view(),
        name = 'news_update',
    ),
    url(
        regex = r'^(?P<slug>[-\w]+)/$',
        view = CategoryHomeView.as_view(),
        name = 'category_detail',
        ),
    url(
        regex = r'^tag/(?P<tag_slug>[-\w]+)/$',
        view = NewsListView.as_view(),
        name = 'news_tag_list',
    ),
    url(
        regex = r'^(?P<category>[-\w]+)/(?P<year>\d{4})/$',
        view = NewsYearArchiveView.as_view(),
        name = "year_archive",
        ),
    url(
        regex = r'^(?P<category>[-\w]+)/(?P<year>\d{4})/(?P<month>\w{3})/$',
        view = NewsMonthArchiveView.as_view(),
        name = "month_archive",
        ),
    url(
        regex = r'^(?P<category>[-\w]+)/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
        view = NewsDayArchiveView.as_view(),
        name = "day_archive",
        ),
    url(
        regex = r'^(?P<category>[-\w]+)/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
        view = NewsDetailView.as_view(),
        name = "article",
        ),
    url(
        regex = r'^publish/(?P<pk>\d+)/$',
        view = publish,
        name = 'publish',
    ),
    url(
        regex = r'^request-publication/(?P<pk>\d+)/$',
        view = request_publication,
        name = 'request_publication',
    ),
)

从shell调用News.all_news.get(pk=1000)可以很好地完成,all_news是标准Django objects管理器的别名,因为我已经使用自定义管理器覆盖了objects。< / p>

我期待的是什么 我看不出有什么理由会失败。我希望视图能够成功返回。

实际发生的事情 标准404页。

限制 由于公司的限制,我无法单独升级到更新的Django。

问题(S) 有没有其他人经历过这个,你是如何解决它的?

1 个答案:

答案 0 :(得分:6)

request-publication/1000视图正在捕获year-archive的请求,因为当三位数pk不匹配时,它与模式r'^(?P<category>[-\w]+)/(?P<year>\d{4})/$匹配。

因此,您获得了404,因为您在1000年中没有发布与slug匹配的项目&#34; requests-publication&#34;。