使用多个正则表达式的django url问题

时间:2017-12-04 11:24:03

标签: django django-views django-urls

我遇到了网址和正则表达式的问题我收到了错误

AttributeError: Generic detail view EmployeeDetailView must be called with either an object pk or a slug.

我要实现的目标是访问来自特定项目的用户详细信息页面

url(r'^project/(?P<pk>[0-9]+)/$',views.ProjectDetailView.as_view(), name='ProjectDetails'),
url(r'^project/(?P<pk1>[0-9]+)/(?P<pk2>[0-9]+)/$',views.EmployeeDetailView.as_view(), name='EmployeDetails'),

我的观点是:

Project detail :
class ProjectDetailView(generic.DetailView, LoginRequiredMixin):
    #import pdb; pdb.set_trace()
    model = Project
    template_name = 'project_details.html'

    def get_context_data(self, **kwargs):
        context = super(ProjectDetailView, self).get_context_data(**kwargs)
        try:
            team_name = Project.objects.get(id=self.kwargs['pk']).team_id.members.all()
            context['team_name'] = team_name
        except AttributeError:
            pass
        return context

class EmployeeDetailView(generic.DetailView, LoginRequiredMixin):
    #import pdb; pdb.set_trace()
    model = MyUser
    template_name = 'Employee_Details.html'

    def get_context_data(self, **kwargs):
        context = super(EmployeeDetailView, self).get_context_data(**kwargs)
        employee_name = MyUser.objects.get(id=self.kwargs['pk'])
        context['employee_name'] = employee_name
        return context

HTML链接:

<span class="fa fa-id-card-o" aria-hidden="true"><a href="{% url 'website:EmployeDetails' pk1 = project.id pk2 = member.id %}"> Show Results</a>

模型: MyUser模型:

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    first_name = models.CharField(max_length=150, blank=True, null=True)
    last_name = models.CharField(max_length=150, blank=True, null=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_hr = models.BooleanField(default=False)
    is_candidate = models.BooleanField(default=False)
    is_employee = models.BooleanField(default=False)
    company = models.CharField(max_length=100, blank=True, null=True)

项目模型:

class Project(models.Model):
    name = models.CharField(max_length=250)
    team_id = models.ForeignKey(Team, blank=True, null=True)
    project_hr_admin = models.ForeignKey('registration.MyUser', blank=True, null=True)
    candidat_answers = models.ManyToManyField('survey.response')

团队模型:

class Team(models.Model):
    team_name = models.CharField(max_length=100, default = '')
    team_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
    members = models.ManyToManyField(MyUser, related_name="members")
你可以帮我算一下吗?你好;)

1 个答案:

答案 0 :(得分:1)

Django不知道如何使用pk1pk2来获取视图的对象。我会覆盖get_object方法并在那里获取对象。

from django.shortcuts import get_object_or_404

# Note mixin should come first
class EmployeeDetailView(LoginRequiredMixin, generic.DetailView):
    model = MyUser
    template_name = 'Employee_Details.html'

    def get_object(self, queryset=None):
        return get_object_or_404(MyUser, pk=self.kwargs['pk2'], project=self.kwargs['pk1'])

    ...

另一种选择是设置pk_url_kwarg = 'pk2'。这告诉Django pk2MyUser对象的主键,因此不需要覆盖get_object。但是,如果您这样做,那么Django将忽略URL中的pk1

class EmployeeDetailView(generic.DetailView, LoginRequiredMixin):
    #import pdb; pdb.set_trace()
    model = MyUser
    pk_url_kwarg = 'pk2'
    template_name = 'Employee_Details.html'