简体   繁体   English

Django 1.5为pk> 999提供404s

[英]Django 1.5 giving 404s for pk > 999

What I'm trying to do I have a simple view that takes a pk as an argument and performs an action. 我正在尝试做的事情我有一个简单的视图,它将pk作为参数并执行操作。 This worked fine until the pks were greater than 999. Now they return 404s. 这个工作正常,直到pks大于999.现在他们返回404s。 I'm trying to fix this. 我正在努力解决这个问题。

What I've tried My view looks like this: 我尝试过的内容我的观点如下:

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()
    ...

And the url regex is like this: 网址正则表达式是这样的:

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

I've also tried: 我也尝试过:

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

which makes it fail on pks < 1000 as expected, but still doesn't work for pk>999. 这使得它在pks <1000时失败,但仍不适用于pk> 999。

The full urls.py is: 完整的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',
    ),
)

Calling News.all_news.get(pk=1000) from a shell works perfectly, with all_news being an alias for the standard Django objects manager because I've overridden objects with a custom manager. 从shell调用News.all_news.get(pk=1000)可以很好地工作, all_news是标准Django objects管理器的别名,因为我用自定义管理器覆盖了objects

What I'm expecting I can't see any reason why this would fail. 我所期待的,我看不出有什么理由会失败。 I'm expecting the view to return successfully. 我期待视图成功返回。

What is actually happening The standard 404 page. 实际发生的事情标准404页面。

Restrictions I can't upgrade to a more recent Django for this alone due to corporate restrictions. 限制由于公司限制,我无法单独升级到更新的Django。

Question(s) Has anyone else experienced this and how did you fix it? 问题有没有其他人经历过这个,你是如何解决的?

The request to request-publication/1000 is being caught by the year-archive view, since it matches the pattern r'^(?P<category>[-\\w]+)/(?P<year>\\d{4})/$ when a three-digit pk would not. 请求request-publication/1000year-archive视图捕获,因为它匹配模式r'^(?P<category>[-\\w]+)/(?P<year>\\d{4})/$当一个三位数的pk不会。

You therefore get a 404 as you have no items published in the year 1000 matching the slug "requests-publication". 因此,您获得404,因为您在1000年中没有发布与“请求 - 发布”子弹匹配的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM