سؤال

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