0

遵循教程,到目前为止,我已经能够解决我所有的小问题,直到我在后端创建自己的“应用程序”以使用 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)
    normalized_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, default=True, 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.

提前感谢您的帮助!

4

1 回答 1

0

在 apps.py 更改name = capstone_project_website.apps.accounts和已安装应用程序的设置中使用“capstone_project_website.apps.AccountsConfig”

于 2021-04-26T17:44:47.737 回答