无法在Django中加载配置文件

2 投票
1 回答
3276 浏览
提问于 2025-04-16 04:23

我正在尝试在默认的用户模型中添加一些额外的字段。我在网上查了一下,做了如下的操作:

我创建了一个新的模型,叫做 UserProfile,并在 /UserProfile/models.py 文件中写了以下内容:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    # This is the only required field
    user = models.ForeignKey(User, unique=True)

    # The rest is completely up to you...
    favorite_band = models.CharField(max_length=100, blank=True)
    favorite_cheese = models.CharField(max_length=100, blank=True)
    lucky_number = models.IntegerField()

然后我又添加了:

 AUTH_PROFILE_MODULE = "newSite.userprofile"

在设置中,我还把 "UserProfile" 加入了 INSTALLED_APPS,这样就可以运行 sqlall 了。

唯一的问题是,当我在命令行中尝试以下操作时:

 >>> from django.contrib.auth.models import User
 >>> user = User.objects.get(username = "user01")
 >>> user.get_profile()

我收到了以下错误信息:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\contrib\auth\models.py", line 367,in get_profile
     raise SiteProfileNotAvailable('Unable to load the profile '

SiteProfileNotAvailable: Unable to load the profile model, check AUTH_PROFILE_MODULE in your project settings

如果有人能帮我解决这个问题,我会非常感激!

1 个回答

4

如果你真的创建了一个叫做 UserProfile 的应用,并且把 models.py 文件放在里面,那么你应该把

AUTH_PROFILE_MODULE = "userprofile.userprofile"

放到你的 settings.py 文件里。

另外,你的 UserProfile 文件夹名字要改成小写:userprofile

撰写回答