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
Related
thats my app urls
from django.urls import path
from . import views
urlpatterns = [
path('', views.tasklist, name='tasks'),
]
thats my projects urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('list.urls')),
]
thats views urls
from django.shortcuts import render
from django.http import HttpResponse
def tasklist(request):
return HttpResponse('TO DO List')
I tried a lot of things but none of them worked
you have to add your app into INSTALLED_APPS list in settings.py
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"))
]
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"
I have used DefaultRouter() and viewset. Here is the code
from rest_framework import routers
from .api import TweetViewset, OwnersTweet
from django.urls import path
router = routers.DefaultRouter()
router.register('', TweetViewset, 'tweets')
router.register('own/', OwnersTweet, 'owner')
And project-level urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls')),
path('tweet/', include('tweets.urls'))
]
When I send a request to '<URL>/tweet/own/' It returned an error Not Found. But <URL>/tweet/ is working. OwnersTweet view also working fine. But I think there is smth wrong with URL. Can you help, please?
I used the path. Now it is working
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.