K
K
kamsikamsa2020-06-24 19:17:42
Django
kamsikamsa, 2020-06-24 19:17:42

How to connect second Django app?

Good afternoon comrades. Briefly about the problem: practicing with django, writing a website with pages \home, \blog, \project. First I wrote the main page startapp mainApp in models.py I wrote

from django.db import models
from datetime import date

from django.urls import reverse

class Article(models.Model):
    """Статьи"""
    title = models.CharField("Название", max_length=100)
    tagline = models.CharField("Слоган", max_length=100, default='')
    description = models.TextField("Описание")
    poster = models.ImageField("Постер", upload_to="poster/")
    year = models.PositiveSmallIntegerField("Дата выхода", default=2019)
    url = models.SlugField(max_length=130, unique=True)
    draft = models.BooleanField("Черновик", default=False)
    

    def __str__(self):
        return self.title
    

    def get_absolute_url(self):
        return reverse("article_namber", kwargs={"slug": self.url})

    class Meta:
        verbose_name = "Статья"
        verbose_name_plural = "Статьи"

Registered this case in admin.py
from django.contrib import admin
from .models import Article

admin.site.register(Article)

In views I wrote:
from django.shortcuts import render
from django.views.generic.base import View

from .models import Article

class ArticlesView(View):
    """вывод статей блога на главную"""
    def get(self, request):
        mainApps = Article.objects.filter(draft=False)
        return render(request, "mainApp/main.html", {"main_list": mainApps})

class ArticlesDetailView(View):
    """полное описание статей"""
    def get(self, request, slug):
        mainApp = Article.objects.get(url=slug)
        return render(request, "mainApp/main_detail.html", {"article": mainApp})

And in Urls I wrote
from django.urls import path

from .import views

urlpatterns = [
    path("", views.ArticlesView.as_view()),
    path("<slug:slug>/", views.ArticlesDetailView.as_view(), name='article_namber')
]


This is how I display articles on the main page (after that I will display only three articles from the blog), everything works, everything is beautiful. But I also want to display the same articles on the blog, with pagination with all the joys, create a new application, copy the yurl and view models one into one, and then register everything twice in the admin panel, this is somehow not according to the canons, therefore, you need not repeat, not write the same thing over and over.
Then I started writing an application for a blog. startapp mainBlog, and then all sorts of ambushes awaited me.
Could you please suggest how to implement this. Is it possible to completely rewrite everything? because all the same, these models belong to the blog, and I will display only three extreme ones on the main one.

After creating a new application, I entered in admin.py
from django.contrib import admin
from mainApp.models import Article

So can you do it at all? I'm in mainBlog and I'm importing from mainApp?

In views.py
from django.shortcuts import render
from django.views.generic.base import View

from mainApp.models import Article

class ArticlesView(View):
    """вывод статей в блог"""
    def get(self, request):
        mainBlogs = Article.objects.filter(draft=False)
        return render(request, "mainBlog/main.html", {"mainB_list": mainBlogs})


To Urls
from django.urls import path

from .import views

urlpatterns = [
    path("blog/", views.ArticlesView.as_view()),
]


The question is do I need to write something in models.py?
in the main Yurl prescribed
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path("", include("mainApp.urls")),
    path("blog/", include("mainBlog.urls")),
]


if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The template moved to the root folder and in the settings wrote 'DIRS': [os.path.join(BASE_DIR, 'templates')],
the templates folder looks like this
>templates
in it include, mainApp, mainBlog and base.html (main template)
in include is header footer
in mainApp is base.html main.html main_detail.html is
in mainBlog is main.html
Now after command runserver writes ModuleNotFoundError: No module named 'mainBlog'
settings, that's my question)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tim, 2020-06-24
@EternalTester

To display the same objects on different pages / applications, Google templatetags. Question: why are you importing the Article table into admin.py? It is already registered in another application. In general, deal with template tags.

S
szelga, 2020-06-25
@szelga

added to INSTALLED_APPS?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question