【发布时间】:2019-12-30 20:06:40
【问题描述】:
我正在尝试形成这样的网址,
http://localhost:8000/mealplan/meals/search/2?from_date=2019-12-29&to_date=2019-12-30&from_time=06:00&to_time=22:00
这是我在 urls.py 中尝试过的
url(r'^meals/search/(?P<user_id>[\w.-]+)/(?P<from_time>[\w.-]+)/(?P<to_time>[\w.-]+)/(?P<from_date>[\w.-]+)/(?P<to_date>[\w.-]+)$', FilterMealList.as_view(), name='filter_meals'),
显然这不起作用。我收到了 404,邮递员上不存在 url。 这也是我基于班级的观点。
class FilterMealList(views.APIView):
def get(self, request, **kwargs):
try:
from_time, to_time, from_date, to_date = kwargs.get('from_time'), kwargs.get('to_time'), kwargs.get(
'from_date'), kwargs.get('to_date')
return Response({"Suceess": "{}, {}, {}, {}".format(from_time, to_time, from_date, to_date)},
status=status.HTTP_200_OK)
except KeyError as e:
return Response({"Failure": "Incomplete query params"}, status=status.HTTP_400_BAD_REQUEST)
所以我的问题, 1. 如何在 django 中定义一个 url 来获取查询参数? 2. 如何在基于类的视图中捕获参数?
如何在 django 中创建一个 url 来获取查询参数
【问题讨论】: