Django, Path return 404 - python

I've got an issue with my Django app, when i try to browse to http://127.0.0.1:8000/list/ it returns a 404 error :
Using the URLconf defined in f_django.urls, Django tried these URL patterns, in this order:
^contact/$
^description/$
^$
myapp/
admin/
The current path, list/, didn't match any of these.
Here my urls.py file :
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from django.urls import include
from . import views
urlpatterns = [
url(r'^contact/$',views.contact),
url(r'^description/$',views.description),
url(r'^$',include('myapp.urls')),
path('myapp/',include('myapp.urls')),
path('admin/', admin.site.urls),
]
My myapp/urls.py file :
from django.conf.urls import url
from django.urls import path
from . import views
urlpatterns = [
url(r'^$',views.index),
url(r'^list',views.list),
url(r'^Articles/(?P<id>[0-9]+)$', views.details),
]
And this line in my settings.py :
INSTALLED_APPS = [
'myapp.apps.MyappConfig',
]
Thanks in advance for your help

Change '^list' to '^list/$', having your urls end with a / is important because Django appends slashes by default to any incoming url which doesn't end in one. You can change this by setting APPEND_SLASH = False in the settings. Also you are including myapp.urls twice in your project level urls.
Also in your include:
url(r'^$',include('myapp.urls'))
You are including urls but in the pattern you write ^$. In regex ^ means the string should start from that position and $ means the string should end at that position. Your pattern for views.list end up being ^$^list/$ which is impossible to match. Basically you are preventing any included urls from being matched if you do this. Remove the $ from there.

Related

Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:

Below are my url patterns from learning logs
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('learning_logs/', include('learning_logs.urls')),
]
And below is the url I'm adding
"""Defines URL patterns for learning_logs"""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = {
# Home page
path('', views.index, name='index'),
# Show all topics
path('topics', views.topics, name='topics'),
# Detail page for a single topic
path(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic', ),
# Page for adding a new topic
path('new_topic', views.new_topic, name='new_topic'),
}
Below is the error I'm getting from my browser
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this
order:
admin/
learning_logs/ new_topic [name='new_topic']
learning_logs/ ^topics/(?P<topic_id>\d+)/$ [name='topic']
learning_logs/ topics [name='topics']
learning_logs/ [name='index']
The current path, learning_logs/topics/(?P1\d+)/, 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.
My Python version environments are
Python 3.10
Django 4.1.1
IDE-PyCharm
Remove the ^ in your url pattern.
This char means: beginning of the full url path. but in your case, it is not the beginning of the full path because, your url start by learning_logs/.
This is what I'm getting from terminal after removing ^ ...
?: (2_0.W001) Your URL pattern 'topics/$' [name='topics'] has a route that
contains '(?P<', begins with a '^', or ends with a '$'. This was likely an
oversight when migrating
to django.urls.path().
?: (2_0.W001) Your URL pattern 'topics/(?P<topic_id>\d+)/$' [name='topic'] has a
route that contains '(?P<', begins with a '^', or ends with a '$'. This was
likely an oversig
ht when migrating to django.urls.path().
And the browser still have a same output

Regular Expressions not resolved after using re_path in django urls

I have a problem with Django urls:
main urls.py
from django.contrib import admin
from django.urls import include, re_path, path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('tutorials.urls')),
]
tutorials/urls.py
from django.urls import re_path
from tutorials import views
urlpatterns = [
re_path(r'^api/tutorials$', views.tutorial_list),
re_path(r'^api/tutorials/(?P<pk>[0-9]+)$', views.tutorial_detail),
re_path(r'^api/tutorials/published$', views.tutorial_list_published)
]
when i go to check API via browser after python manage.py runserver i get:
Looks like regular expressions are not resolved? Any ideas?
You asked the "http://127.0.0.1:8080/api" URL.
Your urlpatterns does not contains any path which could match with this url.
I don't know where you want to go with this url, but if you add
path('api', views.tutorial_list),
In your urlpatterns list, you will go to tutorial_list view for example.
You don't need to use re_path for path without regular expression. just use path function.

Django - The current path, job/all, matched the last one. - But it still returns 404

When I go to this URL http://127.0.0.1:8000/job/all I am always receiving a 404 error with this message:
404 Django Error screenshot
However, when I just go to http://127.0.0.1:8000/ or http://127.0.0.1:8000 it always finds the / (index) route correctly regardless if there is a trailing slash, or not. This issue seems to only happen with the /job/WHATEVER_ELSE_GOES_HERE URLs.
I have the following URLs setup:
My jobs app urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name="index"),
path('job/<int:job_id>', job_details, name="job_detail"),
path('job/all/', all_jobs, name="all_jobs"),
path('job/add/', add_job, name="add_job"),
path('job/my_posted_jobs/', my_posted_jobs, name="my_posted_jobs"),
path('job/delete/<int:job_id>', delete_job, name="delete_job"),
path('job/search', search_job, name="search_job"),
path('job/apply_for_job/<int:job_id>', apply_for_job, name="apply_for_job")
]
My project's urls.py:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('jobs.urls')),
path('user/', include('users.urls')),
path('payments/', include('payments.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In the error message (screenshot) it also says this:
The current path, job/all, matched the last one.
How come it matches a URL but returns 404?
How do I fix it?
Thank you
You have to include the "/" since you have included it in your syntax in your urls.py therefore call the url from your web browser using the link below when you have started the Django web server.
http://127.0.0.1:8000/job/all/

Page not found at /polls

I am a total beginner in "django" so I'm following some tutorials currently I' am watching https://youtu.be/JT80XhYJdBw Clever Programmer's tutorial which he follows django tutorial
Everything was cool until making a polls url
Code of views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
HttpResponse("Hello World.You're at the polls index")
Code of polls\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Code of Mypr\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('polls/',include('polls.urls')),
]
I don't get it I did the same thing but I'm getting error not only polls.In one turtorial he decided to make blog,and again the same error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in
this order:
admin/
The current path, polls/, didn't match any of these.
Please my seniors help me.
Note:I'm using the latest versions of django,windows and as editor I'm using Pycharm.
Already tried(and did not work):
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, 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.
try to access polls/ URL in your browser then you will access the page
Because you have accessed the URL of your project, you have to go to this URL to access your app
try by changing your code like this
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
I was facing the same error. Feel kinda dumb after figuring out how to solve this.
Your urls.py content is all right
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But it must be included in the mysite\mysite\urls.py and not in mysite\urls.py.
That means the inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.

Django url parameter error

Here is my problem :
Using the URLconf defined in Rase.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
bio/<slug:username>/$ [name='bio']
The current path, bio/bussiere/, didn't match any of these.
the url is :
http://localhost:8000/bio/bussiere/
here is my url file :
from django.urls import include, path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path("bio/<slug:username>/$", views.bio, name='bio'),
]
If you have any idea ?
Thanks and regards
You are using Django 2.0.X where url changed pretty much. You don't need to use regex for url when using path() just remove the trailing $ . So the url should be-
path("bio/<slug:username>/", views.bio, name='bio')

Categories

Resources