Skip to main content
 首页 » 编程设计

python之无法在 Django 中加载配置文件

2024年08月12日46itcoder

我正在尝试向默认用户模型添加其他字段。我浏览了互联网并做了这样的事情:

在/UserProfile/models.py 中创建了一个名为 UserProfile 的新模型:

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" 

对于设置,我还在 INSTALLED_APPS 中添加了“UserProfile”以运行 sqlall。

唯一的问题是当我在 shell 中尝试以下操作时:

 >>> 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 

感谢任何解决此问题的帮助!

请您参考如下方法:

如果您确实创建了一个名为 UserProfile 的应用并将 models.py 放在那里,那么您应该放

AUTH_PROFILE_MODULE = "userprofile.userprofile" 

改为进入您的 settings.py 文件。

您应该将 UserProfile 文件夹设为小写:userprofile