问题描述
按照教程,到目前为止,我已经能够解决我所有的小问题,直到我进入我的后端,为可以使用 sqlite 查找的数据创建自己的“应用程序”。
我的工作流程:
> DJANGO-WEBSITE:
> capstone_project_website:
> settings.py
> apps:
> accounts:
> models.py
> apps.py
> requirements:
> scripts:
> manage.py
我的/mymodels.py:
from django.db import models
from django.contrib.auth.models import User
class UserInterest(models.Model):
# name of interest
name = models.CharField(max_length=64,unique=True)
normalized_name = models.CharField(max_length=64,unique=True)
# string function to print the name
def __str__(self):
return self.name
class UserPersona(models.Model):
name = models.CharField(max_length=64,unique=True)
description = models.CharField(max_length=200)
def __str__(self):
return self.name
# Create your models here
class UserProfile(models.Model):
# Owner. Foreign key. Whenever we fetch user,we should fetch the user profile
# related_name = we can access profile through user object aka request.user.profile
user = models.OnetoOneField(User,on_delete=models.CASCADE,related_name="profile")
# settings
is_full_name_displayed = models.BooleanField(default=True)
# details
bio = models.CharField(max_length=500,default=True,null=True)
website = models.URLField(max_length=500,null=True)
persona = models.ForeignKey(UserPersona,on_delete=models.SET_NULL,blank=True,null=True)
interests = models.ManyToManyField(UserInterest,blank=True)
我的/settings.py:
INSTALLED_APPS = [
"django.contrib.admin","django.contrib.auth","django.contrib.contenttypes","django.contrib.sessions","django.contrib.messages","django.contrib.staticfiles","capstone_project_website.apps.accounts",]
我的/apps.py
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = "accounts"
我的终端上运行 python manage.py makemigrations
后出现的错误是:
python manage.py makemigrations
Traceback (most recent call last):
File "/Users/ryandeaver/.pyenv/versions/DjangoWebsite@3.7.3/lib/python3.9/site-packages/django/apps/config.py",line 244,in create
app_module = import_module(app_name)
File "/Users/ryandeaver/opt/anaconda3/envs/3.7.3/lib/python3.9/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1030,in _gcd_import
File "<frozen importlib._bootstrap>",line 1007,in _find_and_load
File "<frozen importlib._bootstrap>",line 984,in _find_and_load_unlocked
ModuleNotFoundError: No module named 'accounts'
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "/Users/ryandeaver/Desktop/django-website/manage.py",line 22,in <module>
main()
File "/Users/ryandeaver/Desktop/django-website/manage.py",line 18,in main
execute_from_command_line(sys.argv)
File "/Users/ryandeaver/.pyenv/versions/DjangoWebsite@3.7.3/lib/python3.9/site-packages/django/core/management/__init__.py",line 419,in execute_from_command_line
utility.execute()
File "/Users/ryandeaver/.pyenv/versions/DjangoWebsite@3.7.3/lib/python3.9/site-packages/django/core/management/__init__.py",line 395,in execute
django.setup()
File "/Users/ryandeaver/.pyenv/versions/DjangoWebsite@3.7.3/lib/python3.9/site-packages/django/__init__.py",line 24,in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/ryandeaver/.pyenv/versions/DjangoWebsite@3.7.3/lib/python3.9/site-packages/django/apps/registry.py",line 91,in populate
app_config = AppConfig.create(entry)
File "/Users/ryandeaver/.pyenv/versions/DjangoWebsite@3.7.3/lib/python3.9/site-packages/django/apps/config.py",line 246,in create
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'accounts'. Check that 'capstone_project_website.apps.accounts.apps.AccountsConfig.name' is correct.
在此先感谢您的帮助!
解决方法
在 apps.py 中更改 name = capstone_project_website.apps.accounts
并在已安装应用的设置中使用“capstone_project_website.apps.AccountsConfig”