AttributeError:类型对象X没有属性Y.

时间:2014-04-27 16:56:18

标签: python django

所以我是Django noob虽然我对Python语法非常熟悉。我一直收到这个错误:

AttributeError at /dashboard/home/
type object 'Member' has no attribute 'dept1'

每次我尝试去我的仪表板/ home / url。

我创建了一个自定义用户模型,如下所示:

from django.db import models    # importing database library from Django   
from django.contrib.auth.models import User

class Member(models.Model):    # table for members' info

    DEPARTMENTS = (
        ('Quiz', 'Quizzing'),
        ('Design', 'Design'),
        ('Elec', 'Electronics'),
        ('Prog', 'Programming'),
    )

    CLASSES = (                 # tuples to store choices for each field
        (9, '9'),               # (actual value to be stored, human-readable value),
        (10, '10'),
        (11, '11'),
        (12, '12'),
    )

    DESIGNATIONS = (
        ('Mem', 'Member'),      
        ('ExecMem', 'Executive Member'),
        ('VicePres', 'Vice President'),
        ('Pres', 'President'),
    )

    user = models.OneToOneField(User)   # to inherit the properties of the base User class in Django, like first_name, last_name, password, username, etc.
    schoolClass = models.IntegerField('Class', choices = CLASSES)
    desig = models.CharField('Designation', max_length = 20, choices = DESIGNATIONS)
    dept1 = models.CharField('Department 1', max_length = 20, choices = DEPARTMENTS)
    dept2 = models.CharField('Department 2', max_length = 20, choices = DEPARTMENTS)
    #proPic = models.ImageField('Profile Picture', upload_to = 'profile_pics')

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

这是来自views.py的我的家庭功能:

@login_required(login_url = '/dashboard/login/')
def home(request):

    noOfPosts = 10

    post_list1 = DepInfo.objects.filter(dept = Member.dept1)[:noOfPosts]
    post_list2 = DepInfo.objects.filter(dept = Member.dept2)[:noOfPosts]

    context = {'post_list1': post_list1, 'post_list2': post_list2}
    return render(request, 'dashboard/home.html', context)

以下是我的信息中心/ home.html模板的必需部分:

<h2> Welcome back, {{ Member.first_name }} </h2>
{% if Member.desig == 'Mem' %}
<h2> Member </h2>
{% elif Member.desig == 'ExecMem' %}
<h2> {{ Member.dept1 }} Executive </h2>
{% else %}
<h2> {{ Member.desig }} - MINET </h2>
{% endif %}
<h2> Departments: </h2>
<h3> {{ Member.dept1 }} </h3>
<h3> {{ Member.dept2 }} </h3>

这是我的admin.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.admin import User

from dashboard.models import Member, DepInfo

class MemberInline(admin.StackedInline):
    model = Member
    can_delete = False
    verbose_name_plural = 'member'

class UserAdmin(UserAdmin):
    inlines = (MemberInline, )

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
#admin.site.register(Member)
admin.site.register(DepInfo)

运行python manage.py shell并在shell中调用print foo.dept1后,它成功运行,但拒绝在此处正常工作。你能告诉我我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:3)

您正在尝试访问Member dept1属性,但您应该从Member类的实例获取该属性

因此,您的视图函数看起来应该更像这样:

current_member = Member.objects.get(user = request.user)
post_list1 = DepInfo.objects.filter(dept = current_user.dept1)[:noOfPosts]
post_list2 = DepInfo.objects.filter(dept = current_user.dept2)[:noOfPosts]