Answer the question
In order to leave comments, you need to log in
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 = "Статьи"
from django.contrib import admin
from .models import Article
admin.site.register(Article)
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})
from django.urls import path
from .import views
urlpatterns = [
path("", views.ArticlesView.as_view()),
path("<slug:slug>/", views.ArticlesDetailView.as_view(), name='article_namber')
]
from django.contrib import admin
from mainApp.models import Article
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})
from django.urls import path
from .import views
urlpatterns = [
path("blog/", views.ArticlesView.as_view()),
]
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)
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question