URL not resolved with APIView in Django REST Framework - django

I'm starting out with DRF and ran into some trouble when trying to set up a simple APIView.
This is the view:
class SongSearchView(views.APIView):
def get(self, request, query, format=None):
return Response(['Justin Bieber - Boyfriend', 'Justin Timberlake - My Love'])
and these are the URL patterns:
router = DefaultRouter()
urlpatterns = patterns('',
url(r'^api/song_search/(?P<query>[a-zA-Z0-9\w]+)/$', views.SongSearchView.as_view()),
url(r'^admin/', include(admin.site.urls)),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', views.index, name='index'),
)
and when I try to access http://127.0.0.1:8000/api/song_search/?query=justin I get a
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/api/song_search/?query=justin
What is the issue? :(

Your Url pattern Regular expression is wrong.. If you still want to stick to your current url pattern , I suggest you change your request url from
http://127.0.0.1:8000/api/song_search/?query=justin to http://127.0.0.1:8000/api/song_search/justin

Related

url is showing in page not found error in django?

I created an endpoint and registered it in urls.py. When I try to hit the endpoint I get a 404 error but on the 404 page the url is shown as one of the patterns django tried to match. Stumped.
api.py
class UpdateLast(viewsets.GenericViewSet, UpdateModelMixin):
def get(self):
return XYZModel.objects.all()
def partial_update(self, request, *args, **kwargs):
if request.user.is_superuser:
with transaction.atomic():
for key, value in request.data:
if key == 'tic':
XYZModel.objects.filter(ticker=request.data['tic']).filter(trail=True).update(
last_high=Case(
When(
LessThan(F('last_high'),
request.data['high']),
then=Value(request.data['high'])),
default=F('last_high')
)
)
urls.py (this is inside the app)
router = routers.DefaultRouter()
router.register('api/dsetting', DViewSet, 'dsetting')
router.register('api/update-last-high', UpdateLast, 'update-last-high')
urlpatterns = [
path('', include(router.urls))
]
urls.py (main)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_app.urls')),
When I hit the end-point api/update-last-high I get a 404 page.
On that page the exact end-point is shown as a pattern that django tried to match.
^api/update-last-high/(?P[^/.]+)/$ [name='update-last-high-detail']
^api/update-last-high/(?P[^/.]+).(?P[a-z0-9]+)/?$ [name='update-last-high-detail']
from rest_framework import routers
from . import views
from django.urls import path, include
router = routers.DefaultRouter()
router.register('api/dsetting', DViewSet, 'dsetting')
router.register('api/update-last-high', UpdateLast, 'update-last-high')
# you forgot to include the router.urls
urlpatterns = [
path('', include(router.urls))
]

Django url/route order not maintained

I have the following in my root URLconf module (there's more, but not important, so left out):
urlpatterns = [
re_path(r'^password-reset-redirect-view/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
password_reset_redirect,
name = 'password_reset_confirm'),
path('', include('search.urls')),
path('', include('customer_portal.urls')),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
Here's the customer_portal.urls:
urlpatterns = [
path('customer/contact/', views.contact),
path('', views.home),
re_path(r"^confirm-email/(?P<key>[-:\w]+)/$", views.email_verification,
name="account_confirm_email"),
]
Here's the rest_auth.registration.urls:
urlpatterns = [
url(r'^$', RegisterView.as_view(), name='rest_register'),
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
name='account_confirm_email'),
]
As you can see both included urls.py urlpatterns have a view named 'account_confirm_email'.
Somewhere in the code this is ran:
url = reverse(
"account_confirm_email",
args=[emailconfirmation.key])
Since customer_portal.urls is included before rest_auth.registration.urls, I expect the route account_confirm_email in customer_portal.urls to be returned by the above reverse method. But instead I get the rest_auth.registration.urls route URL.
Just to be sure I commented out the route in rest_auth.registration.urls, and then I did get the correct URL (customer_portal URL) returned.
It is filled into an email, I check that email and see that I have the wanted url: http://127.0.0.1:8000/confirm-email/......./, instead of: http://127.0.0.1:8000/rest-auth/registration/account-confirm-email/...../
Can anyone tell me why the customer_portal URL isn't the one being reversed in both cases?
Django docs say:
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

Make arbitrary url as homepage

I have such a urls.py configuration in project repository "forum"
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
]
and with article/urls.py as
#Article app's url config
urlpatterns = [
# show the article list
url(r"^list/(?P<block_id>\d+)$", views.article_list, name="article_list"),
I attempt to take "^article/list/1$" as home page instead of "views.index".
How to make it redirect to "^article/list/1$" when I issue request "127.0.0.1:8000"?
You can redirect from your views.index. In your views.py
from django.shortcuts import redirect
def index(request):
return redirect('article:article_list')
which should take you to article/list. You can include that block_id parameter in the redirect function.
try this
redirect(reverse('article:article_list', kwargs={'block_id':2}))
and make sure to add kwargs in function like this
article_list(request,**kwargs):
I assume you want to this to debug your article page.
You have two ways of doing this.
Either redirect your index view to article view
views.py
from django.shortcuts import redirect
def index(request):
#We force a redirect to article_list view, and we pass arguments.
#Notice it has to come first.
redirect ("article_list", article_list_argument_view=1)
#it will redirect to localhost:8000/article/list/1
'''
Your Home page code.
'''
Put the url directly before the include
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
#The order is important.
url(r'^article/list/1$', "article_list", {'article_list_argument_view':1})
url(r'^article/', include('article.urls',namespace='article')),
]

Django can't find redirect url when authenticating user

I am trying to login protect some of my pages, including my dashboard. Here is the view for my dashboard at the root of my site: sitename.com/
#login_required
def index(request):
print(request.session['user_email'])
context_dict = {}
return render(request, 'dashboard/index.html', context_dict)
My project url file:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^job/', include('job.urls')),
url(r'^welcome/', include('welcome.urls')), //the app for logging in
url(r'^$', include('dashboard.urls')), //main dashboard app
# s
]
My dashboard app url file:
urlpatterns = [
url(r'$', views.index, name='index'),
url(r'^signup/$', views.signup, name='signup'),
url(r'^login/$', views.auth_login, name='login'),
url(r'^logout/$', views.user_logout, name='logout'),
]
When I try and logout and then go to / I get a message saying that http://127.0.0.1:8000/welcome?next=/ doesn't match any urls in my project urls file. So the login check is working, it just can't figure out the url when the GET variable next is set.
As you are using django's default login from contrib.auth, try passing next as an extra_context dict.
url(r'^login/$', 'django.contrib.auth.views.login',
{'template_name': 'login.html', 'extra_context': {'next':'/'}})
Another solution is to specify login url in login_required decorator itself
#login_required(login_url='/login/')
def index(request):
print(request.session['user_email'])
context_dict = {}
return render(request, 'dashboard/index.html', context_dict)
Let know if this solve your issue. HTH :)
I finally got it to work. This is my routes:
Welcome app routes (app for logging in/out)
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^signup/$', views.signup, name='signup'),
url(r'^/login/$', views.auth_login, name='login'),
url(r'^/logout', views.user_logout, name='logout'),
]
Main routes for the entire project:
urlpatterns = [
url(r'^welcome', include('welcome.urls')),
url(r'^', include('dashboard.urls')),
]
Now I don't get any errors with the next variable in the url:
http://127.0.0.1:8000/welcome?next=/
I have a javascript file grabbing the value of next and redirecting to it if the user logs in successfully. *I use ajax to authenticate the user.

Not able to add url in Django router

I am not able to add workers URL which is pointing to a method in views.py. In below urls.py configuration, I had created a DefaultRouter, and registered 6 URLs. First 5 are working good(They are Class Based Views), however the last URL(workers, which is method based view) is not working. This URL is not matched with any of the URLs listed in url.conf. Error message I am getting 'Using the URLconf defined in maidFactory.urls, Django tried these URL patterns, in this order:. . . . . . .The current URL, workers/, didn't match any of these.'
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'slots', views.SlotViewSet)
router.register(r'city', views.CityViewSet)
router.register(r'location',views.LocationViewSet,base_name='locationMy')
router.register(r'workers',views.WorkerViewSet,base_name='getWorkersBySlotAndLocation')
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
#url(r'^', include('maidFactory.api.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^auth/', include('rest_framework_social_oauth2.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))]
My method based view is as follows:
def WorkerViewSet(request):
cursor = connection.cursor()
#cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
cursor.execute("select p.wid,p.fname, a.description from workerProfile as p, workerAccount as a where a.isactive=1 and a.wid=p.wid")
row = cursor.fetchone()
return HttpResponse(row)
Your WorkerViewSet is not a actual DRF ViewSet but a Django function-based view returning Django HttpResponse.
You should convert it to a proper DRF viewset and then register your router with this viewset.
Another option is to add this as a url in urlpatterns in your urls file and it should work perfectly.
urlpatterns = [
url(r'^my/url/path/$', my_views.WorkerViewSet), # This will work
....
]