Django KeyError'pk'POST方法

时间:2018-12-26 10:59:39

标签: python html django

我有用于排序对象的post方法

<form method="post" action="{%  url 'package_dashboard' %}">
{% csrf_token %}
    <button  type="submit" name="order_by_title">sortByName</button>
    <button  type="submit" name="order_by_date">sortByDate</button>
</form>

我的views.py

class PackageDashboardView(PackageAccessMixin, ListView):
model = Package
template_name = "packages/dashboard.html"

def get_queryset(self):
    queryset = Package.objects.all().order_by('title', 'version__name'). \
        select_related('title', 'version').defer('title__metadata', 'validation', 'translations', 'diff_fields')
    if "order_by_date" in self.request.GET:
        queryset = Package.objects.all().order_by('-started', 'version__name'). \
            select_related('title', 'version').defer('title__metadata', 'validation', 'translations', 'diff_fields')
    elif 'order_by_title' in self.request.GET:
        queryset = Package.objects.all().order_by('title', 'version__name'). \
            select_related('title', 'version').defer('title__metadata', 'validation', 'translations', 'diff_fields')
    return queryset

我的urls.py

url(r'^packages/$', packages.PackageDashboardView.as_view(), name="package_dashboard"),

错误

  

内部服务器错误:/ packages /回溯(最近一次调用最近):
  文件   “ /home/abdel/Desktop/eclair-packager/eclair_env/lib/python3.6/site-packages/django/core/handlers/exception.py”,   第41行,在内部       response = get_response(request)文件“ /home/abdel/Desktop/eclair-packager/eclair_env/lib/python3.6/site-packages/django/core/handlers/base.py”,   _get_response中的第187行       响应= self.process_exception_by_middleware(e,request)文件“ /home/abdel/Desktop/eclair-packager/eclair_env/lib/python3.6/site-packages/django/core/handlers/base.py”,   _get_response中的第185行       响应= wraped_callback(请求,* callback_args,** callback_kwargs)文件“ /home/abdel/Desktop/eclair-packager/eclair_env/lib/python3.6/site-packages/django/views/generic/base.py”,   视线68       返回self.dispatch(request,* args,** kwargs)文件“ /home/abdel/Desktop/eclair-packager/packager/permission_mixins.py”,   第20行,正在派遣中       返回super()。dispatch(request,* args,** kwargs)文件“ /home/abdel/Desktop/eclair-packager/eclair_env/lib/python3.6/site-packages/django/contrib/auth/mixins.py ”,   第56行,正在派遣中       返回super(LoginRequiredMixin,self).dispatch(request,* args,** kwargs)文件“ /home/abdel/Desktop/eclair-packager/eclair_env/lib/python3.6/site-packages/django/views/generic/ base.py”,   第88行,正在派遣中       返回处理程序(请求,* args,** kwargs)文件“ /home/abdel/Desktop/eclair-packager/packager/permission_mixins.py”,   第96行       obj = self.get_package()文件“ /home/abdel/Desktop/eclair-packager/packager/permission_mixins.py”,   第78行,在get_package中       返回get_object_or_404(Package,pk = self.kwargs ['pk'],** kwargs)KeyError:'pk'[26 / Dec / 2018 11:35:56]“ POST / packages / HTTP / 1.1” 500   109583

1 个答案:

答案 0 :(得分:-1)

在视图或网址部分分配pk(主键)时,您可能会遇到一些问题this may help checkout