Django URL,在URL中传递参数

时间:2018-09-19 10:46:36

标签: python django python-3.x django-rest-framework django-views

我想创建如下网址:

/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'])

2 个答案:

答案 0 :(得分:4)

正如@Umair所说,您将这些键作为 URL查询参数 传递,因此您不必在 {{1} }

就您而言,要获取通过URL传递的数据,请遵循以下代码段

URLPATTERNS

答案 1 :(得分:2)

new_df = 0 0 1 60 2 300 3 320 4 0 5 60 6 300 7 320 8 0 9 60 10 300 11 320 keywordsmood等是location,您不应在url中包括它们,而应通过query params访问它们

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