문제

In my Django urlconf, i'd like to return a 404 for requests probing for scripts. Otherwise, the request is needlessly hitting the database to look for a corresponding FlatPage, since i'm using the FlatPage middleware.

My problem is that the following pattern, which matches correctly, is sending 'php', 'cgi', or 'pl' as the template_name argument to the page_not_found view function.

urlpatterns = patterns('',
    # don't even bother looking up a flatpage for these matches
    (r'\.(php|cgi|pl)$', 'django.views.defaults.page_not_found'), 
    ...
)

What is a way to do that regex "or" logic without sending the match as a parameter to the view?

도움이 되었습니까?

해결책

Non-capturing groups are indicated with (?:...).

다른 팁

You can use a non-capturing group: (?:regex)

So your pattern would be:

(r'\.(?:php|cgi|pl)$', 'django.views.defaults.page_not_found'),

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top