Django-TypeError:load_stations()缺少1个必需的位置参数:'request'?

时间:2019-10-08 19:34:16

标签: python html django django-views

我正在尝试创建相关的下拉菜单,当我尝试编译时出现此错误

File "C:\...\operations\urls.py", line 13, in <module>
    path('ajax/load-stations/', views.load_stations(), name='ajax_load_stations'),
TypeError: load_stations() missing 1 required positional argument: 'request'

我与此部分相关的当前代码是

views.py

class EnterExitArea(CreateView):
    template_name = "operations/enter_exit_area.html"
    form_class = WarehouseForm
    success_url = reverse_lazy('enter_exit_area')

    def form_valid(self, form):
        form.submit_time()
        return super().form_valid(form)


def load_stations(request):
    work_area_id = request.GET.get('work_area')
    stations = StationNumber.objects.filter(work_area_id=work_area_id).order_by('name')
    return render(request, 'operations/station_number_dropdown_list_options.html', {'stations': stations})

我的网址是

urls.py

urlpatterns = [
    url(r'enter-exit-area/$', EnterExitArea.as_view(), name='enter_exit_area'),
    path('ajax/load-stations/', views.load_stations(), name='ajax_load_stations'),
]

什么可能导致此错误?

1 个答案:

答案 0 :(得分:2)

您错误地使用了基于函数的视图。

此行: path('ajax/load-stations/', views.load_stations(), name='ajax_load_stations'),

应该是

path('ajax/load-stations/', views.load_stations, name='ajax_load_stations'),

因为您没有在基于函数的视图调用中包含方括号。