django python - 更改slug不会重定向到404页面

时间:2014-02-28 21:28:33

标签: python django

以下是我的观点之一,我在我的网址上得到了理想的结果,即

本地主机/雇员/ 0001 / reportees

但是当我更改我的网址时,它不会将我重定向到404页面,它会显示相同的结果,上面和下面的网址都给我相同的结果。当slug更改时,如何使页面转到404。注意:两者都显示相同的结果,表示url对生成的结果没有影响

本地主机/雇员/ 0002 / reportees

class EmployeeReporteeView(LoginRequiredMixin, ListView):
    model = employee
    template_name = "employee_reportees.html"
    slug_field = "slug"
    def get_context_data(self, **kwargs):
        self.base_qs = super(EmployeeReporteeView, self).get_queryset()
        context = super(EmployeeReporteeView, self).get_context_data(**kwargs)
        context['primary_list'] =     self.base_qs.filter(primary=self.request.user.employee.empid)
        context['secondary_list'] = self.base_qs.filter(secondary=self.request.user.employee.empid)
        return context

只是为了给出一个想法,我在下面的视图中有详细信息视图,当我更改slug时,它会重定向到404 当员工0001登录时,此页面显示他的个人资料 本地主机/雇员/ 0001 localhost / employee / 0002将重定向到404,因为他看不到其他员工的详细信息

class EmployeeDetailView(LoginRequiredMixin, DetailView):
    model = employee
    template_name = "employee_detail.html"
    context_object_name = "employee_detail"
    def get_queryset(self):
        base_qs = super(EmployeeDetailView, self).get_queryset()
        return base_qs.filter(email=self.request.user)

# urls.py
     url(r"^(?P<slug>[\w-]+)/$", EmployeeDetailView.as_view(), name="employee_detail"),
     url(r"^(?P<slug>[\w-]+)/reportees/$", EmployeeReporteeView.as_view(), name="employee_reportees"),

1 个答案:

答案 0 :(得分:0)

您的查询集仅基于一个会话值(此处为request.user.employee.empid)。因此,每个slug都会给出相同的结果,对应于当前记录的员工 如果要显示URL中写入的slug的报告者,则需要修改查询集以使用slug参数。类似的东西:

def get_context_data(self, **kwargs):
    self.base_qs = super(EmployeeReporteeView, self).get_queryset()
    context = super(EmployeeReporteeView, self).get_context_data(**kwargs)
    context['primary_list'] = self.base_qs.filter(primary=self.kwargs['slug'])
    context['secondary_list'] = self.base_qs.filter(secondary=self.kwargs['slug'])
    return context