I've been using Django since Django 1, and I've always used the same URL patterns (except when we switched from url to path).
Now I'm having an issue with 404 errors. I'll give you my Project URLS, and App URLS, and you tell me what am I doing wrong:
Project:
urlpatterns = [
path('b/', include('booking.urls')),
]
Booking App:
urlpatterns = [
path('book/<int:s>/<str:d>/', views.book, name="book"),
path('fb/', views.finalize_booking, name="finalize_booking"),
]
When I try to call {% url "finalize_booking" %}, it gives me a 404 error.
You should add forward slash at the start of of your string.
urlpatterns = [
path('/book/<int:s>/<str:d>/', views.book, name="book"),
path('/fb/', views.finalize_booking, name="finalize_booking"),]
Related
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
I have an issue where I try to go to my redirect page and get a NoReverseMatch when though the URL is there? Any idea how to fix this?
I have checked that the "schema" url works and it correctly supplies the openapi schema, but the other page simply can't understand the url.
URLS:
urlpatterns = [
path("schema/", SpectacularAPIView.as_view(), name="schema"),
# Optional UI:
path("docs/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
]
Errors:
For reverse url pathing, you have to use {% url api:schema %}. It's specified as namespace next to include('api.urls') or inside app urls, just above urlpatterns - like app_name = "api".
I have a Django backend that returns json data. I'm able to get data back on my localhost but got a 404 on production server. I'm running nginx in front of gunicorn server. Any ideas why I'm getting a 404? Shouldn't this be able to work to retrieve json data, or do I need to use django rest framework and implement viewsets to make this work?
Not Found
The requested URL /about was not found on this server.
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about', about.get_info),
]
about.py
from django.http import JsonResponse
def get_info(req):
return JsonResponse({"test": "hello"})
The problem is inside url.py. The way the rules are defined currently, it would only allow you to open about/ and admin/, i.e. with the / at the end. To fix this, you can define the URLs as following:
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^about/$', about.get_info),
]
Now you should be able to use both admin/ and admin to access the page.
I am trying to learn Django and I am currently stuck in an issue.
I created an app Contact and run the server, I get the error.
The error page displayed by server:
The urls.py file in the app Contact
urls.py in conatct
When the pattern in urls.py is
urlpatterns =[url(r'^$', views.form, name ='form')]
it works properly, but not with other pattern shown in the picture
Your help would be greatly appreciated.
The Page not found error message tells you what went wrong: For the URL (/contact) you requested, Django was unable to find a suitable view. Because you have debugging enabled, you get some information, including a list of registered views.
First things first: You probably have url(r'^contact/', include('contact.urls')) somewhere in your top level urls.py. This makes the URLs defined in the contact/urls.py available under the prefix /contact.
With
urlpatterns = [
url(r'^form/', views.form, name='form'),
]
in contact/urls.py you are telling Django that you want urls starting with contact/form/ to be handled by views.form.
Consequently, when you access http://localhost:8000/contact/ in your browser, there is no view associated with that URL, hence the 404. Your view is reacting to to http://localhost:8000/contact/form, not http://localhost:8000/contact.
When you change the URL pattern to
urlpatterns = [
url(r'^$', views.form, name='form'),
]
you modify the URL views.form reacts to.
I'm learning Django, and so far I always had to use URL's like
projectname/appname/viewname
but what if I don't want appname to appear in the URLs for the "default" app, how can I configure my urls so that
projectname/viewname
will load the view viewname from my default app?
P.S. : Of course my primary goal is to be able to use the URL projectname/ to load the default view for the default app.
Details
Currently my ProjectName/urls.py has this:
urlpatterns = patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp1/', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),
url(r'^myapp2/', include('myapp2.urls', namespace='myapp2', app_name='myapp2')),
)
so when I deploy my project to Heroku, and visit myproject.heroku.com, I get the error :
Page not found (404)
Request Method: GET
Request URL: https://myproject.herokuapp.com/
Using the URLconf defined in MyProject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^myapp1/
^myapp2/
I know this is supposed to be, but how do I fix (or hack) this to get myproject.heroku.com to work?
If not possible, how can I redirect the homepage to myproject/myapp1/defaultview ?
Thanks in advance !
my app's urls.py looks like this :
urlpatterns = patterns('myapp1.views',
url(r'^view1/$', 'view1', name='view1'), # the default view
url(r'^view2/(?P<oid>\d+)/(?P<pid>\d+)/$', 'view2', name='view2'),
)
Edit
After trying #Wallace 's suggestion url(r'^$', include('myapp1.urls', namespace='myapp1', app_name='myapp1')), and hitting the homepage, I now get the error:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^$ ^view1/$ [name='view1']
^$ ^view2/(?P<oid>\d+)/(?P<pid>\d+)/$ [name='view2']
^myapp2/
Tried changing your project urls.py with:
url(r'', include('myapp1.urls', ...)
This will include all urls from myapp1.urls where they all append to /.
The reason why r'^$' won't work is because the regex ends with $ which means there can only be 1 x url /, and because your app1.urls only has 2 urls defined and without a / or ^$ equivalent, the url resolver will then fail.
But be aware of url clashes, if your project has a ^view1/$ url it will clash to your app1's view1 for example.
Try not including your appname in the regular expression.
url(r'', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),