访问 Django 1.3 中的子类用户配置文件模块

发布于 2024-11-10 04:37:01 字数 1433 浏览 0 评论 0原文

我正在尝试在 django 1.3 中创建两种用户类型。我使用以下 models.py 对 AUTH_PROFILE_MODULE 进行子类化:

class Member(models.Model):
    ROLE_CHOICES = (
        (0, 'Guide'),
        (1, 'Operator'),
    )
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=60)
    role = models.IntegerField(choices=ROLE_CHOICES)


class Guide(Member):
    bio = models.TextField(blank=True)
    experience = models.TextField(blank=True)
    image = models.ImageField(blank=True, upload_to='images')
    fileupload = models.FileField(blank=True, upload_to='files')

    def __unicode__(self):
        return self.user.username

    def get_absolute_url(self):
        return '/profiles/guides/%s' % self.user.username


class Operator(Member):
    bio = models.TextField(blank=True)
    image = models.ImageField(blank=True, upload_to='images')

    def __unicode__(self):
        return self.user.username

    def get_absolute_url(self):
        return '/profiles/operators/%s' % self.user.username

我正在使用基于通用类的视图,并且可以让 ListView 为 Guide 和 Operator 模型工作,但我无法让 DetailView 工作。我的views.py如下:

class GuideDetailView(DetailView):
    model = Guide
    context_object_name = 'guide'
    template_name = 'members/guide_detail.html'


class GuideListView(ListView):
    model = Guide
    context_object_name = 'guides'
    template_name = 'members/guide_list.html'

知道可能缺少什么吗?

I am trying to create two user types in django 1.3. I am subclassing the AUTH_PROFILE_MODULE with the following models.py:

class Member(models.Model):
    ROLE_CHOICES = (
        (0, 'Guide'),
        (1, 'Operator'),
    )
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=60)
    role = models.IntegerField(choices=ROLE_CHOICES)


class Guide(Member):
    bio = models.TextField(blank=True)
    experience = models.TextField(blank=True)
    image = models.ImageField(blank=True, upload_to='images')
    fileupload = models.FileField(blank=True, upload_to='files')

    def __unicode__(self):
        return self.user.username

    def get_absolute_url(self):
        return '/profiles/guides/%s' % self.user.username


class Operator(Member):
    bio = models.TextField(blank=True)
    image = models.ImageField(blank=True, upload_to='images')

    def __unicode__(self):
        return self.user.username

    def get_absolute_url(self):
        return '/profiles/operators/%s' % self.user.username

I am using generic class based views and can get the ListView to work for the Guide and Operator models I cannot get the DetailView to work. My views.py is as follows:

class GuideDetailView(DetailView):
    model = Guide
    context_object_name = 'guide'
    template_name = 'members/guide_detail.html'


class GuideListView(ListView):
    model = Guide
    context_object_name = 'guides'
    template_name = 'members/guide_list.html'

Any idea what might be missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少跟Wǒ拽 2024-11-17 04:37:01

要么提供一个查询集:

class GuideDetailView(DetailView):
    queryset = Guide.objects.all()

要么重写 DetailView 的 get 方法:

class GuideDetailView(DetailView):
    def get(self):
        return "Everything you want, maybe: Guide.object.get(id=1)"

在 urls.py 中给出这个:

url(r'^(?P<my_id>\d)/

您需要重写 get,如下所示:

class GuideDetailView(DetailView):
    def get(self, request, **kwargs):
        # lookup Guide Id in your database and assign it object
        self.object = Guide.objects.get(pk=kwargs.get('my_id'))
        # add object to your context_data, so that you can access via your template
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)
, GuideDetailView.as_view(),),

您需要重写 get,如下所示:

Either provide a queryset:

class GuideDetailView(DetailView):
    queryset = Guide.objects.all()

or override the get Method of DetailView:

class GuideDetailView(DetailView):
    def get(self):
        return "Everything you want, maybe: Guide.object.get(id=1)"

Given this in your urls.py:

url(r'^(?P<my_id>\d)/

You need to override get, like this:

class GuideDetailView(DetailView):
    def get(self, request, **kwargs):
        # lookup Guide Id in your database and assign it object
        self.object = Guide.objects.get(pk=kwargs.get('my_id'))
        # add object to your context_data, so that you can access via your template
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)
, GuideDetailView.as_view(),),

You need to override get, like this:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文