I found an interesting thing I don't know how to solve:
urlpatterns = patterns('',
url(r'^$', 'my_app1.views.index', name='index'),
url(r'^path1/path2/', 'my_app1.api.method1')
// or url(r'^path1/path2', 'my_app1.api.method1')
)
I don't want the urls to contain "/" at the end.
If I go to http://localhost:8000/path1/path2 it redirects me to http://localhost:8000/path1/path2/ with the "/" at the end. Whatever I do to get rid of the "/" at the end of the url, I fail at. It seems there's no way to do so, unlike in Rails. Is it true?
You can control it using settings.APPEND_SLASH.
By default, it's True. Change the value to False in the project settings file.
APPEND_SLASH = False
But if you set it, accessing the page /path/path2 will result 404 error. You need to change the url pattern:
url(r'^path1/path2/?', 'my_app1.api.method1') # To match url with/without trailing /
Related
This is a silly problem. I just created a project and have been trying to figure out this problem.
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="index.html")),
url(r'^about$', TemplateView.as_view(template_name="about.html")),
url(r'^contact$', TemplateView.as_view(template_name="contact.html"), name="contact"),
url(r'^test$', TemplateView.as_view(template_name="test_start"), name="test_start"),
url(r'^test/sample$', TemplateView.as_view(template_name="test_start"), name="test_start"),
]
is included into
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('frontend.urls'))
]
When I go to localhost:8000/about, I get redirected to localhost:8000/about/ and there I get 404 Not Found.
UPDATE: I added more URLs into my URLconf.
UPDATE 2: I meant to not include trailing slashes. My apologies.
UPDATE 3: I opened the same URL in Firefox and the URL works like I intend. Could this be a problem with redirection and browser cache?
Did you enable the append_slash setting ?
https://docs.djangoproject.com/en/dev/ref/settings/#append-slash
using this may help to make it more explicit and is recommended throughout the django tutorials
url(r'^about/$', TemplateView.as_view(template_name="about.html")),
EDIT:
Deactivate the APPEND_SLASH settings (False) and use
url(r'^about$', TemplateView.as_view(template_name="about.html")),
First, I found out that Chrome automatically adds trailing slash at the end of the URL
Trailing URL Slashes in Django
So if you don't have a trailing slash on your URLS, a 404 redirect will show if you're using Chrome, but not if, say, Firefox.
Then from the comment of knbk from here,
How Django adds trailing slash
I made sure I had the CommonMiddleware class in setting.py and added 'APPEND_SLASH = False'
Then, cleared Chrome's cache, and problem solved!
You can just remove the $ from your regex, this indicates the end of line
url(r'^about', TemplateView.as_view(template_name="about.html")),
You could also just include a slash to your regex, since Django has a APPEND_SLASH setting which will issue a redirect
url(r'^about/$', TemplateView.as_view(template_name="about.html")),
if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended.
Change your url pattern for "about" to:
url(r'^about/?$', TemplateView.as_view(template_name="about.html")),
Without the /?, the regex ^about$ matches a string containing exactly the word "about".
I've been making some URLs in Django, including them and so on. Everything looks fine, but no matter what i do i always end up with a 404 on some of the simplest URLs.
For example I can browse myapp/0 abd myapp/1/details/, but i get 404'd at myapp/foo
So here are my urlconf :
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
and myapp urlconf :
urlpatterns = [
url(r'^foo/$ ', FooView.as_view()),
url(r'^(?P<bar_id>\d+)/$', BarByIdView.as_view()),
url(r'^(?P<bar_id>\d+)/details/$', BarDetailsByIdView.as_view()),
]
And when i tryp myapp/foo Django shows me the following urls list :
^admin/
^myapp/ ^foo/$
^myapp/ ^(?P<bar_id>\d+)/$
^myapp/ ^(?P<bar_id>\d+)/details/$
In the myapp.conf you have added a / at the end of the url pattern
url(r'^foo/$ ', FooView.as_view()
This must be viewed with /myapp/foo/ and not /myapp/foo because the first one matches the regex where as the second one won't.
Your URL is /myapp/foo/, not /myapp/foo. Note the trailing slash.
If you want both to work, ensure you have the APPEND_SLASH setting set to True and the CommonMiddleware enabled.
I have a very simple Django project with an url-pattern, similar to this one:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^log/', include('apps.logging.urls')),
url(r'^OPTION1/?', index, name="index"),
url(r'^OPTION2/?', index, name="index"),
url(r'^$', index, name="index"),
url(r'^.+/?', RedirectView.as_view(url="/", permanent=False)),
)
So essentially, I want the index view to be rendered, when the url is either /OPTION1, /OPTION2 or /. I also tried to put this logic into one single regular expression, but I always ended up with the same problem:
As soon as I'm working in production mode on an actual server, the emtpy url patter (^$), gets always evaluated in addition to the normal one. So if someone navigates to /OPTION1, the index view will actually called twice, once with / and once with /OPTION1 as path.
As soon as I comment out the empty url patter everything works fine. I also have no redirects (except the one that you see in the url pattern, but I can comment it out and the problem persists), that could cause this problem and the status code returned is always 200.
So has someone any idea what's going on?
u can use this to redirect to ur home page upon xyz option:
url(r'^/(?P<slug>[^\.]+)/$', anotherIndex),
and inside the view u can use this:
from django.shortcuts import HttpResponseRedirect
def anotherIndex(request, slug):
redirect_url = "/"
if slug == "OPTION1":
redirect_url = "/OPTION1"
elif slug == "OPTION2":
redirect_url = "/OPTION1"
return HttpResponseRedirect(redirect_url)
I hope it helps
I'm attempting to pass a string from the end of a url to a view function, but I'm getting the entire url passed to render_form() i.e. if I input "myurl.me/prepend/identifier" I get "prepend/identifier/" when I just want "identifier"
These are my urls.py files, the top one is located in an app directory, the bottom one is in the project dir:
urlpatterns = patterns('',
url(r'^(?P<identifier>.+)$', views.render_form),
)
--
urlpatterns = patterns('',
url(r'^prepend/', include('the_ones_above.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I think it might have somehting to do with the split over 2 urlpatterns in 2 separate files, any ideas what im doing wrong?
Thanks!
edit: the view looks like: def render_form(request, identifier=None):
oh, and this is the latest django (from git)
2nd edit: removed the rather superfluous 1st url, behavior is still the same
I will suggest you to simply write you url like.
url(r'^(?P<identifier>[\w]+)/$', views.render_form),
[\w] will tell you that you can pass word character (A-Za-z0-9_).
I have what I think is a pretty simple URL configuration for a public API I'm building in django:
# public API, in urls.py
(r'^api/', include('api.urls'))
# in api/urls.py
api_key_patterns = patterns('api.geoprocessing',
url(r'^land-use/', 'landUse', name='geoprocessing_land_use'))
urlpatterns = patterns('',
url(r'^(?P<api_key>(.+))/', include(api_key_patterns)))
However, when I make a request to /api/123/land-use/ Django's URL resolver cannot find a match. When I debug the urls it has tried, it resolves the above to this:
^api/ (?P<api_key>(.+))/ land-use/ [name='geoprocessing_land_use']
Notice the spaces between (?P<api_key>(.+)) and land-use. Why are these spaces inserted for each include, and how can I get a match?
Update
If I hard-code a url for ^api/(?P<api_key>(.+))/land-use/ I am able to get a match.
Also, if I add a character after the trailing / the URL match also works:
api_key_patterns = patterns('api.geoprocessing', url(r'^and-use/$', 'landUse',name='geoprocessing_land_use'))
urlpatterns = patterns('', url(r'^(?P<api_key>(.+))/l', include(api_key_patterns)))
Try:
urlpatterns = patterns('api.geoprocessing',
url(r'^land-use/(?P<api_key>(.+))/$', 'landUse',
name='geoprocessing_land_use'))
)
I'm not sure what you're trying to accomplish by including a pattern when it doesn't seem like you need to be doing that.
Looks like using a (.+) group within my regular expression was forcing the django URL resolver to look for a character that wasn't there. This always parses when there are characters following the trailing slash, but when this group sits at the end of the url segment, a space is tagged at the end.
I resolved this by exchanging the (.+) group with a simple word matcher \w+:
(r'^api/(?P<api_key>\w+)/', include('api.api')),