Answer the question
In order to leave comments, you need to log in
How to import a model from another app in django?
Has project shop
in it apps blog, home
I am trying to get model from blog app
from django.shortcuts import render
from datetime import datetime
from django.http import HttpResponse
from shop.blog.models import Post
def get_home_page(request):
# return HttpResponse("start")
template = 'home/home.html' # шаблон по умолчанию
posts = Post.objects.filter(published_date__lte=datetime.now(), published=True)
return render(request, template, {
"posts": posts
})
ModuleNotFoundError: No module named 'shop.blog'
from django.shortcuts import render
from datetime import datetime
from django.http import HttpResponse
from ..blog.models import Post
def get_home_page(request):
# return HttpResponse("start")
template = 'home/home.html' # шаблон по умолчанию
posts = Post.objects.filter(published_date__lte=datetime.now(), published=True)
return render(request, template, {
"posts": posts
})
from ..blog.models import Post
ValueError: attempted relative import beyond top-level package
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question