简体   繁体   中英

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. This worked fine until the pks were greater than 999. Now they return 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.

The full urls.py is:

# 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.

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.

Restrictions I can't upgrade to a more recent Django for this alone due to corporate restrictions.

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.

You therefore get a 404 as you have no items published in the year 1000 matching the slug "requests-publication".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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