why its showing empty path didnt match any of these - django

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.

Related

How to redirect folder request to a subfolder with htaccess and GREP

I have about 200 folders that contain pdfs and docs. They should all be redirected to a subfolder in the same directory. There are also folders and files in the same directory that should not.
The folders all have the same name-structure (123-name-name with - or _ mixed) which match the following with GREP in BBEdit:
(\d{3}(_|-).+)
So I tried something like:
RedirectMatch 302 ^/([\d{3}(_|-).+)]/(.*)/?$ /subfolder/$2
placed in the same folder as the ones I want to redirect.
I have no idea how to set the match correctly.
A URL like example.com/images/123-name-name/somefile.pdf (or doc, docx) should be found at example.com/images/subfolder/123-name-name/somefile.pdf
But the code above results in a Internal Server Error.
It would be great if someone could help me in that.
You could use:
^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$
to capture both parts of the url in capture groups, and replace with $1kunden/$2, so your line will look like:
RedirectMatch 302 ^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$ $1kunden/$2
This would insert kunden/ in between the rest of the url and the file name.
If you want to specify that the filename must have an extension (ie, include at least 1 .) you could replace the regex with:
^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*?\..*?)$
EDIT My bad, to prevent the recursion, you can use a negative look-ahead to ensure the subfolder doesn't already exist in the path:
RedirectMatch 302 ^((?!.*subfolder\/).*?\/)(\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$ $1subfolder/$2$3
This will prevent a url that already contains subfolder/ from redirecting again.

Django exclude a single url from adding slash

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.

Django greedy characters missing in reverse urls

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.

Django URL with catch all except /admin and /blog

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.

Django URL (Page Not Found)

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-]+