0

This happens when I try to rn server

This photo describes the project structure Django - Python


views.py

Just when I try to import "Post" model, I have this error and I can't reach any page.
If I don't import any model in views.py everything looks good..
Could you help me to fix this issue?

from .models import Post
def home_view(request):
    latest_posts = Post.objects.order_by('pub_date')[:3]
    return render(request, 'home.html', {'Latest': latest_posts})

models.py

from django.db import models

# Create your models here.


class Author(models.Model):
    name = models.CharField(max_length=120)
    brief_life = models.TextField(blank=True, null=True)
    Date_of_birth = models.DateField()

    def __str__(self):
        return self.name


class Type(models.Model):
    name = models.CharField(max_length=120, unique=True)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.name


class Post(models.Model):
    title = models.CharField(max_length=120, unique=True)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(decimal_places=2, max_digits=10000)
    pub_date = models.DateField()
    copy_num = models.IntegerField(default=1)
    for_buy = models.BooleanField(default=True)
    author = models.ForeignKey(Author, on_delete=True)
    p_type = models.ForeignKey(Type, on_delete=True)

    def __str__(self):
        return self.title

apps.py

from django.apps import AppConfig
class PostsConfig(AppConfig):
    name = 'posts'

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'posts',
]



MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'project.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'project.wsgi.application'

url.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path

from src.posts.views import home_view, about_view, details_view, post_info_view

urlpatterns = [
    path('', home_view, name='home'),
    path('about/', about_view, name='about'),
    path('details/', details_view, name='details'),
    path('info/', post_info_view, name='info'),
    path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
9
  • Did you add the app name (here 'posts') to the INSTALLED_APPS in your settings.py file? Commented Oct 29, 2018 at 17:55
  • Yes I added it to settings.py and everything works right but when I try to import my model in views.py it gives me that error.<br/> Commented Oct 29, 2018 at 18:02
  • This is INSTALLED_APPS in settings.py: ` INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'posts', ]` @WillemVanOnsem Commented Oct 29, 2018 at 18:03
  • is your views.py in same directory ? try from posts.models import Post Commented Oct 29, 2018 at 19:21
  • I tried it, but nothing changed @PankajSharma Commented Oct 29, 2018 at 19:38

2 Answers 2

1

You have your ForeignKey on_delete set up wrong. True is not a valid option. Refer to the Model Field reference ForeignKey field to set a valid option. If you want to delete when foreign key is removed, use models.CASCADE

Also, your installed app is posts. In your urls you are importing from src.posts.views. Try getting rid of the src. and it should fix your problem.

Sign up to request clarification or add additional context in comments.

3 Comments

I still have the same error message: "INSTALLED_APPS." % (module, name) RuntimeError: Model class src.posts.models.Author doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Did you run your migration?
yes, I deleted all migration files and execute python manage.py makemigrations and then python manage.py migrate but I still have the same problem when adding from .models import Post to views.py file.
0

I have fixed it by removing the spaces by tabs in code beautifying and its working

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.