Just starting out with Django and figuring out the URL dispatcher. From what I can see in the docs the following regular expression should catch news/story/2012/10/23/this-is-my-first-story
urlpatterns = patterns('news.views',
url(r'^$', 'index'),
url(r'^news/story/(?P<year>\d{4})/(?P<month>\d){2}/(?P<day>\d){2}/(?P<title_key>\w+)/$', 'story'),
)
However, I am getting the following error...
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^news/
^$
^news/
^news/story/(?P<year>\d)/(?P<month>\d)/(?P<day>\d)/(?P<title_key>\d)/$
^admin/doc/
^admin/
The current URL, news/story/2012/10/23/this-is-my-first-story, didn't match any of these.
First, you're only accepting ONE digit per pattern. You need to modify it to \d+ or more appropriately, \d{4} for year and \d{2} for month and day.
Second, the last pattern, for title_key is set to only accept numeric (\d), if you want a slug, you should use [\w-]+
Related
Using the URLconf defined in My_Ecom_Project.urls, Django tried these URL patterns, in this order:
admin/
account/
^static/(?P.)$
^media/(?P.)$
The empty path didn't match any of these.
Add a path with the urlpattern being '/'. You can also use '' if your webserver handles stripping final slashes. As #Willem Van Onsem mentions you're simply not handling the path you're webserver is passing to Django.
I've added 2 views to my Django urls config for SEO but when I view them on my site it redirects them to the url I setup but with an added slash. I know Django likes to tidy up URLs with a slash (and I like this) but is there a way to exclude a single url or a few urls from this feature?
...
url(r'^robots\.txt/$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^sitemap\.xml/$', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml')),
...
These append slash to the urls resulting in robots.txt/ and sitemap.xml/ not robots.txt and sitemap.xml
If you don't want slashes at the end of robots.txt and sitemap.xml, then simply remove the trailing slashes from those regexes.
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^sitemap\.xml$', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml')),
The URLs /robots.txt and /sitemap.xml will then match, and Django will not append the slash.
I am trying to use the following rule to map urls in django
url(r'^(?P<permalink>[a-zA-Z0-9_-]*)/?$', views.page, name='page'),
This should match pages like
site.com
site.com/super-awesome-page/
This works however the reverse urls provided by the url template tag are missing the trailing / i.e. "site.com/page" these do get captured pattern but I want my links to show up in my page correctly how can I get this to work correctly.
I would have expected since the trailing slash is greedy it should be included in the reverse url.
The trailing slash is optional in your regex, so django doesn't generate it for you for the reverse URL.
The easiest solution is probably to make the trailing slash non-optional. With the default settings, django will redirect the non-slash version to the slash-version. Easiest to just standardize on that.
I want to make a catch all URL in my Django project...
It works fine, but now i can't go to my /admin and /blog page .. any idea how I can do this?..
My urls.py
url(r'^admin/', include(admin.site.urls)),
(r'^blog/$', 'apps.blog.views.index'),
(r'^([a-zA-Z0-9\-\_]*)', 'apps.review.views.show_search'),
You don't need to handle this manually with a RedirectView. You likely need to add the CommonMiddleware to your MIDDLEWARE_CLASSES settings if it is not already in there.
(inserting 'django.middleware.common.CommonMiddleware' into that list or tuple). It usually goes near the top of that declaration. Its documentation is here:
https://docs.djangoproject.com/en/1.5/ref/middleware/#django.middleware.common.CommonMiddleware
You also need to be sure APPEND_SLASH is set to True in your settings.
Furthermore, your last pattern is likely to match anything, which I suspect could be the real culprit of your problem. A URL without a trailing slash has to fail to match any patterns in order for it to automatically redirect to one with a trailing slash per that middleware. I think you can fix the last URL pattern by appending /$ to that pattern
The admin and blog urls have trailing slashes i.e. /admin/ and /blog/. The urls with the trailing slashes should work at the moment.
If you want to make the urls work without the slash, you could add entries for /admin and /blog, and use RedirectView to redirect.
I have 7 urls:
url: /v/3b89441db7986135e5eb9e1debf0cc23
url: /v/3b89441db7986135e5eb9e1debf0cc23/login
url: /v/3b89441db7986135e5eb9e1debf0cc23/logout
url: /v/3b89441db7986135e5eb9e1debf0cc23/access
url: /v/3b89441db7986135e5eb9e1debf0cc23/delete-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/finalise
How could I write a rewrite rule that if these URLS are not matched, I redirect the user to another domain?
For example the part after /v/ is always a 32 character MD5 string. the :id part is always a number.
If you could give me a regex (regex has alas never been my forte) example for
/v/3b89441db7986135e5eb9e1debf0cc23
and
/v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
that would be excellent.
This regex matches your urls:
/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?
In english...
/v/ is a literal
[a-f0-9]{32} means 32 hex digits
/[a-z-]+(/\d+)? means "/" then at least 1 of (any lowercase letter or a dash) then "/" then some digits
surrounding a regex in (...)? means either one or none of them
FYI, this regex matches all urls given in question reasonably tightly
If you want to not match, use this:
^(?!/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?$)
How about this:
\/v\/[a-f0-9]{32}(\/(login|logout|access|delete-attendee\/:\d+|edit-attendee\/:\d+|finalise)?
This will only match your accepted urls. You should adjust for your flavor or regex and appropriate escape chars.
You said that you want to redirect if URLs do not match, so first you must make a rule that matches all valid URLs, then prepend ! to negate the match.
RewriteEngine On
RewriteRule !^v/[0-9a-f]{32}(/(login|logout|access|delete-attendee/\d+|edit-attendee/\d+|finalise))?$ http://your-other-domain/ [R]
The above rule should be placed in the .htaccess file present in the root directory of your website.