gpt4 book ai didi

python - 属性错误: type object X has no attribute Y

转载 作者:行者123 更新时间:2023-11-30 23:24:38 28 4
gpt4 key购买 nike

所以我是一个 Django 菜鸟,尽管我非常熟悉 Python 语法。我不断收到此错误:

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

每次我尝试访问我的仪表板/主页/网址时。

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

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的我的home函数:

@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后,itt成功运行,但在这里无法正常工作。你能告诉我我的代码有什么问题吗?

最佳答案

您正在尝试访问成员dept1属性,但您应该从实例获取该属性成员(member)类别。

所以,你的 View 函数应该看起来更像这样:

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]

关于python - 属性错误: type object X has no attribute Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326247/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com