0

I want to create url like:

/api/foodfeeds/?keywords=BURGER,teste&mood=happy&location=2323,7767.323&price=2

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^api/foodfeed/(?P<keywords>[0-9.a-z, ]+)/(?P<mood>[0-9.a-z, ]+)/(?P<location>[0-9]+)/(?P<price>[0-9]+)/$', backend_views.FoodfeedList.as_view()),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

views.py

class FoodfeedList(APIView):
    # permission_classes = (permissions.IsAuthenticated,)
    def get(self,request,keywords,mood,location,price):
        print(request.GET['keywords'])
4
  • What's the error you are getting? What is that you are expecting? Commented Sep 19, 2018 at 10:51
  • 2
    You can't pass the paramaters like that. Those are query parameters and are not specified in the url. You should implement the logic in views.py and check the query parameters there. Commented Sep 19, 2018 at 10:53
  • what will be the good way to pass parameters ? Commented Sep 19, 2018 at 10:59
  • 1
    django-rest-framework.org/api-guide/requests/#query_params Commented Sep 19, 2018 at 11:37

2 Answers 2

4

As @Umair said, you're passing those keys as URL query parameters, so you don't have to mention it in URLPATTERNS

In your case, to get the data you're passing through the URL, follow the below code snippet

#urls.py
urlpatterns = [
                  path('admin/', admin.site.urls),
                  url(r'^api/foodfeed/', backend_views.FoodfeedList.as_view()),
              ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


#views.py
class FoodfeedList(APIView):
    # permission_classes = (permissions.IsAuthenticated,)
    def get(self, request):
        print(request.GET) # print all url params
        print(request.GET['keywords'])
        print(request.GET['mood'])
        # etc
Sign up to request clarification or add additional context in comments.

Comments

2

Those keywords, mood, location, etc are query params you should not include those in url, rather you should access them via request.query_params

Reference : http://www.django-rest-framework.org/api-guide/requests/#query_params

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.