Django Python Page not found (404) - python

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.

Related

What the meaning of ^ for url

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

404 Error with Django URL even though it is included in url patterns

Here's the error I'm getting:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/bet/4/update
Using the URLconf defined in chessbet.urls, Django tried these URL patterns, in this order:
admin/[name='bet-home']
bet/<int:pk> [name='bet-detail']
bet/<int:pk>/update/> [name='bet-update']
about/ [name='bet-about']
bet/new/ [name='bet-create']
register/ [name='register']
login/ [name='login']
logout/ [name='logout']
yourprofile/ [name='yourprofile']
^media/(?P<path>.*)$
The current path, bet/4/update, didn't match any of these.
Now from what I see the current path is equivalent to the path I laid out for bet-update. What am I doing wrong here?
Here is my urls.py:
urlpatterns = [
path('', BetListView.as_view(), name = "bet-home"),
path("bet/<int:pk>", BetDetailView.as_view(), name="bet-detail"),
path("bet/<int:pk>/update/>", BetUpdateView.as_view(), name="bet-update"),
path('about/', views.about, name = "bet-about"),
path("bet/new/", BetCreateView.as_view(), name="bet-create")
]
Bet detail which does something very similar works fine but bet update does not.
Any help would be much appreciated.
It seems that you have odd /> in update path should be
path("bet/<int:pk>/update", BetUpdateView.as_view(), name="bet-update"),

Polls application -- django tutorial not working

I followed the instructions in the tutorial
I made one change for debugging, here are the files:
mysite1\urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^abc/', include('polls.urls')),
url(r'^polls/', admin.site.urls),
url(r'^admin/', admin.site.urls),
]
File: mysite1\polls\urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^&', views.index, name='index'),
]
Now if I go to the site http://127.0.0.1:8000/polls/ then it shows the login page same as going to the site http://127.0.0.1:8000/admin/.
However, if I go to the site http://127.0.0.1:8000/abc/ it gives me the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/abc
Using the URLconf defined in mysite1.urls, Django tried these URL patterns, in this order:
^abc/
^polls/
^admin/
The current URL, abc, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Can someone guide me what I am doing wrong?
In mysite1\polls\urls.py It should be
url(r'^$', views.index, name='index'),
Notice that your code had an & instead of $. $ indicates a end-of-string match character.
I had a few issues with this, maybe it'll help someone. My VS code was running python 2.7 so the first line on page urls.py in the web project folder (not the polls folder) I had to change to "django.conf.urls import include, url" from "from "django.urls import include, path". I had to include .conf. and urls, path was not working and it was not working without "conf".
Another issue I had on this same page is I had to correct it to "urlpatterns = [url('', include('hello.urls'))..." from "urlpatterns = [path('', include("hello.urls"))". Path was not working so I got this to work.
The third thing is, Notice that there is a '' in the code. It should be blank otherwise when you go to url http://127.0.0.1:8000/ it doesn't redirect to views.py in the polls folder.
I hope this helps someone. Thanks

Django URL weird behaviour

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'),
]

Django: index.html url cannot be found (Django polls tutorial)

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.

Categories