'RuntimeError: Model class mainpage_authentication.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
Before applying models to my project everything was okay. But when I just applied them, the system crushed. I'm a freshmen learning Django, so will be grateful for an explanation, and a solution, if possible.
My code:
admin.py
from django.contrib import admin
from .models import User
admin.site.register(User)
apps.py
from django.apps import AppConfig
class AuthenticationPageConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'mainpage_authentication'
forms.py
from django import forms
from .models import User
class AuthenticationLoginForm(forms.Form):
class Meta:
model = User
fields = ['username', 'password']
models.py
from django.db import models
class User(models.Model):
username = models.CharField(max_length=100)
password = models.CharField(max_length=100)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainpage_authentication',
]
views.py
from django.shortcuts import render
from django import views
from mainpage_authentication.forms import AuthenticationLoginForm
class MainPageView(views.View):
def get(self, request):
return render(request, 'mainpage.html')
class AuthenticationLoginPageView(views.View):
def get(self, request):
form: AuthenticationLoginForm = AuthenticationLoginForm()
return render(request, 'authentication-page.html', {"form": form})
Solution 1:[1]
You can use
from mainpage_authentication.models import User
instead of
from .models import User
Basically use an absolute import instead of a relative import for that model.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arun T |
