error url using django and rest framweork - python

Error when I try to call some id endpoint for my urls.
For example, the view in rest_framework:
I try to enter http://127.0.0.1:8000/categoria/4 or 5 and I see an error.
The current path, categoria/4, didn't match any of these. (error 404)
I am using the rest_framework for this.
file urls.py
from django.urls import path, include
from django.conf.urls import url
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^doctor$', views.DoctorList.as_view()),
url(r'^doctor(?P<pk>[0-9]+)$', views.DoctorDetail.as_view()),
url(r'^paciente$', views.PacienteList.as_view()),
url(r'^paciente(?P<pk>[0-9]+)$', views.PacienteDetail.as_view()),
url(r'^categoria$', views.CategoriaList.as_view()),
url(r'^categoria(?P<pk>[0-9]+)$', views.CategoriaDetail.as_view()),
url(r'^examen$', views.ExamenList.as_view()),
url(r'^examen(?P<pk>[0-9]+)$', views.ExamenDetail.as_view()),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

I think this might help in your case:
from django.urls import path, include
from django.conf.urls import url
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^doctor$', views.DoctorList.as_view()),
url(r'^doctor/(?P<pk>[0-9]+)$', views.DoctorDetail.as_view()),
url(r'^paciente$', views.PacienteList.as_view()),
url(r'^paciente/(?P<pk>[0-9]+)$', views.PacienteDetail.as_view()),
url(r'^categoria$', views.CategoriaList.as_view()),
url(r'^categoria/(?P<pk>[0-9]+)$', views.CategoriaDetail.as_view()),
url(r'^examen$', views.ExamenList.as_view()),
url(r'^examen(?P<pk>[0-9]+)$', views.ExamenDetail.as_view()),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
You should check for exact pattern - categoria/<regex> but you missed the slash before regex. Hope this helps.

Related

how to access the url after Django rest frame router

I am unable to access the url after path("", include(router.urls)),
i would like to render a template after the load
i am using django rest framework and i want to access the url after the rest fromework router and render a template
from django.urls import path, include
from rest_framework import routers
from .views import NSE_DATA_VIEW_SET, AddCoi
router = routers.DefaultRouter()
router.register(r"", NSE_DATA_VIEW_SET)
urlpatterns = [
path("", include(router.urls)),
path("add", AddCoi)
]
Image:
from django.urls import include, path
urlpatterns = [
path("nsedata/", include("api.nsedata.urls")),
path("coidata/", include("api.coidata.urls"))
]

Why DRF giving me 404

I am keep getting 404 in DRF whenever I send request from postman even though the url is correct.
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('backend.urls'))
]
backend/urls.py
from django.urls import path, include
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('/post', views.PostView, basename='post')
urlpatterns = [
path('', include(router.urls)),
]
From postman I am sending post request on the endpoint http://127.0.0.1:8000/post but I am keep getting 404 not found error. What am I doing wrong?
This is what works for me.
Create a app named backend using python manage.py startapp {app_name} where app_name is backend
Add the newly created app to INSTALLED_APPS in your project settings.py
In your project url.py file do this:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("backend.urls")),
]
In your app urls.py file. (where app is backend):
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register('post', views.PostView, basename='post')
app_name = 'backend'
urlpatterns = [
path('', include(router.urls)),
]
Then in your app(backend) views.py define PostView
This should work.The endpoint should be at :
http://localhost:8000/post/ .
if your using port 8000
Try to change backend/urls.py:
import views
from django.urls import path, include
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('post', views.PostView, basename='post')
urlpatterns = router.urls

Error in Django: Using the URLconf defined in django_test.urls, Django-3 tried these URL patterns, in this order:

I've tried setting these URL's in this project, but I get a 404 when trying to access the registration.html file and the api-auth URL that is related to a REST API
For reference:
index = app
django_test = site
index/urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', views.homepage, name="homepage"),
path('/register', views.registration, name="registration"),
path('api', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace="rest framework")),
]
django_test/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')),
path('admin/', admin.site.urls)
]
index/views.py
from django.shortcuts import render
def homepage(request):
return render(request,"home/homepage.html")
def registration(request):
return render(request, 'home/registration.html')
All of our HTML files are located in index/templates/home directory
Can you try changing name="registration" to name="register" ?
Change the URL "register/" to "/registration"

The current path, didn't match any of these

So I have code below that is in django 1.8
from django.conf.urls import patterns, url
from account import views
from django.contrib.auth import views as auth_views
urlpatterns = patterns('',
url(r'^$', views.index, name='profile'),
url(r'^api/get_users/(?P<term>.*)', views.get_users),
url(r'^leaderboard/(?P<board_type>.*)', views.leaderboard),
url(r'^admintools/(?P<action>.*)', views.admintools),
)
I modified it to django 2.2
from django.conf.urls import url
from . import views
from django.urls import re_path,path
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.index, name='profile'),
path('api/get_users/(?P<term>.*)', views.get_users),
path('leaderboard/(?P<board_type>.*)', views.leaderboard),
path('admintools/(?P<action>.*)', views.admintools),
]
I get the error The current path account/admintools, didn't match any of these
"one of the easy solutions" to this problem is, use re_path(...) instead of path()
from django.urls import re_path
from account import views
urlpatterns = [
re_path(r'^$', views.index, name='profile'),
re_path(r'^api/get_users/(?P<term>.*)', views.get_users),
re_path(r'^leaderboard/(?P<board_type>.*)', views.leaderboard),
re_path(r'^admintools/(?P<action>.*)', views.admintools),
]
The re_path(...) function will do the same thing as the Django url(...) did.

In Django, how do you keep a module's url configurations encapsulated inside the module?

Currently, I import url configurations into my Django project with:
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
from rest_framework import routers
from greeter.views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'greeters', GreeterViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include(ROUTER.urls)),
]
Is there a way where I can move these parts of the code:
ROUTER = routers.DefaultRouter()
ROUTER.register(r'greeters', GreeterViewSet)
into a separate file in greeter/urls.py?
And still keep these URLs:
GET /greeter/ to fetch list of greeters
POST /greeter/ to create a new greeter
I have tried:
my_project/urls.py
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^greeters/', include('greeter.urls')),
]
greeter/urls.py
from django.conf.urls import url, include
from rest_framework import routers
from .views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'^', GreeterViewSet)
urlpatterns = [
url(r'^', include(ROUTER.urls)),
]
But got:
$ curl -H 'Accept: application/vnd.api+json; indent=2' -X POST http://localhost:8000/greeters/
{
"errors": [
{
"detail": "Method \"POST\" not allowed.",
"source": {
"pointer": "/data"
},
"status": "405"
}
]
}
In case it helps, here is my original question that has been resolved:
Method "POST" not allowed with Django Rest Framework
Update:
With the help of the answers, I was able to arrive at this solution:
my_project/urls.py
from django.conf.urls import include
from django.contrib import admin
from django.urls import re_path
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('greeters/', include('greeter.urls')),
]
greeter/urls.py
from django.conf.urls import include
from django.urls import re_path
from rest_framework import routers
from .views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'', GreeterViewSet)
urlpatterns = [
re_path(r'', include(ROUTER.urls)),
]
With this:
all of the greeter url configuration is kept inside of the greeter
module
the project url configuration ties 'greeter' resource to the
greeter module
it seems like additional greeter views can be added to
the greeter url configuration (I haven't tested this)
I think this is as good as I can get it. Thanks again for all of the help :)
As I said to you before, the Router needs a prefix. You need to remove that prefix from your main URLs and use it in the router itself.
main:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('greeter.urls')),
]
app:
ROUTER = routers.DefaultRouter()
ROUTER.register(r'^greeter/', GreeterViewSet)
urlpatterns = ROUTER.urls
(Since you don't have any URLs other than the router ones, you don't need to use include there, you can just use the router urls directly.)
Also note, this whole thing is almost certainly not what you want to do; it means you can never have any URLs other than those for your viewset.
Removing the ^ (the Caret symbol) from regex expression will do the job
# greeter/urls.py
# your code
urlpatterns = [
url(r'', include(ROUTER.urls)),
]

Categories