It might be too simple question.
I have this urlpatterns in urls.py
urlpatterns = [
url(r'^s3direct/', include('s3direct.urls')),
path('admin/', admin.site.urls),
]
localhost/admin works, but localhost/s3direct shows the 404 error.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8099/s3direct
Using the URLconf defined in djang_test.urls, Django tried these URL patterns, in this order:
^s3direct/
admin/
The current path, s3direct, didn’t match any of these.
(I use runserver at port 8099)
The ^ in this case is a Regex-specific operator, meaning that it only matches the beginning of the string.
I believe that your problem is that you actually need to request http://localhost:8099/s3direct/ -- You're missing the trailing backslash
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 just started my journey with Django and I can't figure out what I did wrong. Sorry for that simple question.
inz/urls.py
urlpatterns = [
url(r'', include('planner.urls')),
url(r'^admin/', include(admin.site.urls)),]
planner/urls.py
urlpatterns = [
url(r'^$', views.main_page),
url(r'^/student/$', views.student, name='student'),]
And my href in base.html:
Student
And my error:
Request URL: http://127.0.0.1:8000/student/
Using the URLconf defined in inz.urls, Django tried these URL patterns, in this order:
^$
^/student/$ [name='student']
^admin/
The current URL, student/, didn't match any of these.
Remove the leading slash from ^/student/$:
url(r'^student/$', views.student, name='student'),
FYI, in the URL dispatcher docs there is a related example:
There’s no need to add a leading slash, because every URL has that. For example, it’s ^articles, not ^/articles.
I've been trying to code a simple app, and came across this weird issue. Look below, my urls.py, first:
This is my urls.py file:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^(?P<label_slug>[\w-]+)/$', views.tasks, name='tasks'),
url(r'^add-label/$', views.add_label, name='add_label'), # this URL is acting funny
url(r'(?P<label_slug>[\w\-]+)/add_task/$', views.add_task, name='add_task'),
]
so whenever I access my third url (/add) it is going to a 404 error page, but when I add something like label/add, it seems to work. Can someone tell me how to fix this thing?
This is what the 404 error page says:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/tasks/add/
Raised by: tasks.views.tasks
No TaskLabel matches the given query.
Your URL '^(?P<label_slug>[\w-]+)/$' is swallowing the /add-label thinking it's the slug of a task and calls the view tasks.views.tasks
List URL regex in their ascending order of genericity
As a good practice you should always put the more generic regex URLs at the bottom, since the URLs are evaluated in that order
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^add-label/$', views.add_label, name='add_label'),
url(r'^(?P<label_slug>[\w-]+)/$', views.tasks, name='tasks'),
url(r'(?P<label_slug>[\w\-]+)/add_task/$', views.add_task, name='add_task'),
]
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 can't seem to get the tutorial at https://docs.djangoproject.com/en/1.6/intro/tutorial03/ to work. I get the following error when trying to access http://127.0.0.1:8000/index.html or http://127.0.0.1:8000/
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, index.html, didn't match any of these.
I have /mysite/urls.py set to:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And index.html is in /polls/templates/polls/index.html
The URLs in your urls.py are the ones you can go to; so either
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/polls/
Provided both those subapps (admin and polls) define a view for the empty string ("^$"), which I believe both of them do.
The fact that one of those views happens to use a template named "index.html" doesn't mean that that filename is also used in URLs.