如果URL中存在URL,我正在尝试获取该URL,但一直收到以下错误:
django.utils.datastructures.MultiValueDictKeyError:'sc'
情况是URL有时可能如下所示:
/allot-graph/
有时:
/allot-graph/?sc='foo'
在我的职能中,我正在做这件事:
class AllotmentbyMonth(APIView):
def get(self, request):
q = request.GET['sc']
if q:
print("q", q)
dataset = some query
else:
dataset = some query
当URL类似于/allot-graph/?sc='foo'
时,您将使用q=request.GET.get('sc' , '')
。
sc是您想要的参数,如果找不到sc,则"是默认值。
另一种方法是使用URLconf,然后从正则表达式中捕获的内容作为参数(或命名参数(传递给函数。
例如:(r'^ allot-graph/(?P< sc >w{0,50})/$', views.profile_page,)
那么在你看来,你会有
def profile_page(request, sc):
# Rest of the method