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
Related
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
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"
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.
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)),
]
I started working with Django yesterday and I have a little error. I searched on the internet but I cant find the answer for my problem. I made the main app and then I make a little app and tried to link it to the main. Here is my code
urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('^$', include('personal.urls'));
]
in my personal.urls I have
from django.urls import path, include
from . import views
ulrpatterns = [
path('^$', views.index, name="index");
]
and in views.index I have
def index(request):
return render(request, 'personal/home.html')
Any help would be awesome, thank you!!!
You are using the wrong tool here: since django-2.0, there are basically two ways to specify a URL:
with a regex through re_path [Django-doc], url [Django-doc] is an "alias" of this, but will probably eventually get deprecated; and
by using a specific "path pattern" syntax with path [Django-doc]
Here you somehow mixed the two, and use path(..) function for a "regex-specified" URL.
Using path for path pattern-based URLs
We can also fix it by using path:
# urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import path, include
from . import views
ulrpatterns = [
path('', views.index, name="index");
]
Using re_path for regular expression-based URLs
We can also fix it by using re_path instead:
# urls.py
from django.contrib import admin
from django.urls import re_path, include
ulrpatterns = [
re_path('admin/', admin.site.urls),
re_path('^$', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import re_path, include
from . import views
ulrpatterns = [
re_path('^$', views.index, name="index");
]