Django Rest Framework SimpleRouter inclusion inserts ^ into url pattern - python

I have DRF application with urls defined using SimpleRouter.
# project/app/urls.py:
from rest_framework.routers import SimpleRouter
from .viewsets import ExampleViewset, TopViewset
router = SimpleRouter()
router.register(r"example/", ExampleViewSet, basename="example")
I imported this router to main project urls file.
# project/urls.py:
from project.app.urls import router as app_router
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("app/", include(app_router.urls)),
]
GET localhost:8000/app/example/ returns 404.
Opening localhost:8000/app/example/ in browser returns this error page:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/app/example/
Using the URLconf defined in backend.urls, Django tried these URL patterns, in this order:
admin/
app/ ^example//$ [name='example-list']
app/ ^example//(?P<pk>[^/.]+)/$ [name='example-detail']
The current path, app/example/, 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.
I expected app/example in URLconf, but instead there is app/ ^example. I think ^ means beginning of line. So my question is, why this happened and how to fix it?

I had to remove trailing slashes from registrations:
router.register(r"example", ExampleViewSet, basename="example")
Now correct url is generated.

Related

Django, Path return 404

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.

Getting error when I am trying to fetch my main url patterns

I am a beginner in Django I have created a Django project in that I have included two more application when I did add urls.py file in both Applications both are working well but when I am fetching my main admin URL it is giving an error
Page not found (404)
Request Method: GET
URL: http://127.0.0.1:8000/
the URLconf defined in mac.urls, Django tried these URL patterns, in this order:
admin/
shop/
blog/
The empty path 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.
when I am fetching this URL in http://127.0.0.1:8000/ i am getting an error it is working for http://127.0.0.1:8000/shop/
here is my main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
Your django app has 3 routes:
http://127.0.0.1:8000/admin/ goes to django admin app
http://127.0.0.1:8000/shop/ goes to your shop app
http://127.0.0.1:8000/blog/ goes to your blog app
And since you have no configuration for http://127.0.0.1:8000, you see an error instead.
You can see that in the error, when django tries to match your url with list of available urls.
If you want to get admin app on url http://127.0.0.1:8000, change urls.py to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
It's generally not advisable to set admin app at root url - it has it's own system of urls inside (admin/<app_name>/<model_name>), so chances are it will shadow your urls and make the unreachable.
Create a view that will be your front page.
From there you should link to the other areas of your website.
Don't direct that to admin, that's ridiculous.
You've created a Django project and started two apps. You should have a project-level urls.py file and then an app-level urls.py file for each of your apps.
To explain that in greater detail lets say our Django project is called config and our two apps are called app1 and app2. Your project-level urls.py file, which will be located at config/urls.py, could contain the following:
# config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home.html'),
name='home'),
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
]
In this file we specify a route for our admin panel, which on your local server will be located at http://127.0.0.1:8000/admin. We've also specified a home route, the second path with the empty string. This means when you navigate to http://127.0.0.1:8000/ you will be directed to your home page (for the example above I just used a generic built-in view). It's not a good idea to route immediately to your admin panel.
We've also included paths to our other two apps. What these two lines essentially say is: "include the urls from this other app". Now we need to create two urls.py files, one for each of our apps. In this example I'll just focus on the urls.py file for app1:
# app1/urls.py
from django.urls import path
from .views import AppContentView
urlpatterns = [
path('content/', AppContentView.as_view(),
name='app_content'),
]
This is a view that you would have to create, but what we've now done is we've created one path that will be located at http://127.0.0.1:8000/app1/content. In fact any new paths we create in this file will always begin with http://127.0.0.1:8000/app1/, because we've already told Django in our project-level urls.py to include the urls from the app1 urls.py file so we've essentially prefixed all of these paths with /app1/.
If you think of urls configurations like a tree it might help too:
Project Level Url Configs.
|
|
|
___________________________
| |
| |
App 1 Url Configs. App 2 Url Configs.

django 2.2.3-Django 404 error ('Page not found') after runserver

My project is named 'tweetme', and when I runserver I get this error.
Using the URLconf defined in tweetme.urls, Django tried these URL patterns, in this order:
^admin/
^static/(?P<path>.*)$
The empty path 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.
urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += (static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT))
I expect to shows: the install worked successfully! Congratulation
but its shows me: Page not found at/
You have defined only two url patterns in your config. So only those will successfully work as expected. For url / to work correctly, you will have to add additional url pattern as mentioned on Django documentation using " " or "/" in relative url
https://docs.djangoproject.com/en/2.2/topics/http/urls/#url-namespaces-and-included-urlconfs

Django catch-all URL that still works with APPEND_SLASH

I am trying to route all unknown URLs to a single view. However, in doing this my known URLs only work when they do not have a trailing slash, despite setting APPEND_SLASH to True in "settings.py".
Here is some code:
settings.py:
APPEND_SLASH = True
ADMIN_URL = "admin/"
urls.py:
from django.conf import settings
from django.contrib import admin
from django.urls import include, path, re_path
from myapp.views import my_catch_all_view
urlpatterns = [
path(settings.ADMIN_URL, admin.site.urls),
re_path(r"^.*", my_catch_all_view),
]
If I go to "localhost:8000", I correctly get routed to my catch-all view.
And if I go to "localhost:8000/foobar/", I correctly get routed to my catch-all view.
And if I go to "localhost:8000/admin/", I correctly get routed to the admin view.
But, if I go to "localhost:8000/admin", I incorrectly get routed to my catch-all view.
I have seen this answer, but unfortunately that doesn't work for me.
Any suggestions?

Including a Django app's url.py is resulting in a 404

I have the following code in the urls.py in mysite project.
/mysite/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/$', include('mysite.gallery.urls')),
)
This results in a 404 page when I try to access a url set in gallery/urls.py.
/mysite/gallery/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/browse/$', 'mysite.gallery.views.browse'),
(r'^gallery/photo/$', 'mysite.gallery.views.photo'),
)
404 error
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^gallery/$
The current URL, gallery/browse/, didn't match any of these.
Also, the site is hosted on a media temple (dv) server and using mod_wsgi
Remove the $ from the regex of main urls.py
urlpatterns = patterns('',
(r'^gallery/', include('mysite.gallery.urls')),
)
You don't need gallery in the included Urlconf.
urlpatterns = patterns('',
(r'^browse/$', 'mysite.gallery.views.browse'),
(r'^photo/$', 'mysite.gallery.views.photo'),
)
Read the django docs for more information
Note that the regular expressions in this example don't have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

Categories

Resources