I want to match the following url
/calendar/entry/add/2013/11/23/05:30
my urls.py
url(r'^entry/add/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<time>\d+:\d+)/$', 'view')
and on main urls.py I have
url(r'^calendar/',include('mycal.urls'),
but it isnt working...and I get a no match. What is the right regexpr for this....
The url /calendar/entry/add/2013/11/23/05:30 does not contain trailing /.
Remove the trailing / from the url pattern or make it optional to make it match with the url
url(r'^entry/add/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<time>\d+:\d+)/?$', 'view')
# ^^
Related
I have created the following urlpatters
urlpatterns=[
url(r'(?P<user_name>[a-zA-Z]+)/$', views.profile_view,
kwargs=None, name='userprofile'),
url(r'(?P<user_name>[a-zA-Z]+)/interests/$',views.interest,name='interests')
]
But when I enter the url localhost:8000/mainuser/interest/ it treat it as first url and opens profile_view. It is clearly matching '/'. Help me with this.
You should begin your URL patterns with the start of line character ^. Because you are not including this character the regex for the first pattern matches any URL that ends with 1 or more characters followed by a forward-slash.
urlpatterns=[
url(r'^(?P<user_name>[a-zA-Z]+)/$', views.profile_view, kwargs=None, name='userprofile'),
url(r'^(?P<user_name>[a-zAZ]+)/interests/$',views.interest,name='interests'),
]
I have the following urlpatterns:
url(r'^api/daily-means/$', views.daily_means.as_view(), name='daily_means'),
url(r'^api/daily-means/sites/(?P<url>\w+)/$', views.site_daily_means.as_view()),
url(r'^api/daily-means/pollutant/(?P<poll>\w+)$/', views.pollutant_daily_means.as_view()),
The first two work fine. The last one show work the same as the second one but it does not. Im not that great with regex and urlpatterns but I assume there is something with the second url pattern which is stopping the last one from running. Can anyone else see a reason for this?
Django will append the end slash if it is not provided. In your regex, you are matching without the end slash.
url(r'^api/daily-means/pollutant/(?P<poll>\w+)$/', views.pollutant_daily_means.as_view()),
The following URL pattern should work(after including the end slash as a part of URL match).
url(r'^api/daily-means/pollutant/(?P<poll>\w+)/$', views.pollutant_daily_means.as_view()),
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'),
All of the examples I can find of urlpatterns for django sites have a separate entry for incoming urls that have no leading slash, or the root folder. Then they handle subfolders on each individual line. I don't understand why a simple
/?
regular expression doesn't permit these to be on one simple line.
Consider the following, let's call the Django project Baloney and the App name is Cheese. So in the project urls.py we have something like this to allow the apps urls.py to handle it's requests...
urlpatterns = patterns('',
(r'^cheese/', include('Baloney.Cheese.urls')),
)
then inside of the Cheese apps urls.py, I don't understand why this one simple line would not trigger as true for all incoming url subpaths, including a blank value...
urlpatterns = patterns('',
(r'^(?P<reqPath>.*)/?$', views.cheeseapp_views),
)
Instead, it matches the blank case, but not the case of a value present. So...
http://baloneysite.com/cheese/ --> MATCHES THE PATTERN
http://baloneysite.com/cheese/swiss --> DOES NOT MATCH
Basically I want to capture the reqPath variable to include whatever is there (even blank or '') but not including any trailing slash if there is one.
The urls are dynamic slugs pulled from the DB so I do all the matching up to content in my views and just need the url patterns to forward the values along. I know that the following works, but don't understand why this can't all be placed on one line with the /? regular expression before the ending $ sign.
(r'^$', views.cheeseapp_views, {'reqPath':''}),
(r'^(?P<reqPath>.*)/$', views.cheeseapp_views),
Appreciate any insights.
I just tried a similar sample and it worked as you wrote it. No need for /?, .* would match that anyway. What is the exact error you are getting? Maybe you have your view without the request parameter? I.e. views.cheeseapp_views should be something like:
def cheeseapp_views(request, reqPath):
...
Edit:
The pattern that you suggested catches the trailing slash into reqPath because * operator is greedy (take a look at docs.python.org/library/re.html). Try this instead:
(r'^(?P<reqPath>.*?)/?$', views.cheeseapp_views)
note it's .*? instead of .* to make it non-greedy.
Im trying to work out a url that will match domain.com\about-us\ & domain.com\home\
I have a url regex:
^(?P<page>\w+)/$
but it won't match the url with the - in it.
I've tried
^(?P<page>\.)/$
^(?P<page>\*)/$
but nothing seems to work.
Try:
^(?P<page>[-\w]+)/$
[-\w] will accept a-z 1-9 and dash