J
J
jenyasafronov2020-12-22 09:40:17
Django
jenyasafronov, 2020-12-22 09:40:17

How to pass data from multiple models to DetailView?

views.py

class DetailView(DetailView):
    model = Page
    context_object_name = 'Page'
    template_name = 'page/pagescr.html'


urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from page import views


urlpatterns = [
    path('admin/', admin.site.urls),
    path('grappelli/', include('grappelli.urls')), # grappelli URLS
    path('', views.index, name='index'), 
    path('<slug:slug>/', views.DetailView.as_view(), name='pagesc'), 
]

models.py
class Page(models.Model):
    page_title = models.CharField('Title страницы', max_length = 200)
    slug = models.SlugField('URL страницы', primary_key=True, max_length=250, unique=True)
    page_text = models.TextField('Содержимое страницы')
    
    def __str__(self):
        return self.page_title
        
    def get_absolute_url(self):
        return reverse('detail', args=[str(self.slug)])  

class Content(models.Model):
    info = models.CharField(max_length = 200)
    ban_scr_sc = models.CharField(max_length = 200)


What is the essence, by means of DetailView I create dynamic pages from the Page model. But it is necessary, in addition to the Page model, to transfer data to the DetailView from other models (for example, from the Content model). How to implement correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-12-22
@jenyasafronov

Either the Page must have a link to the Content, or get the desired Content and pass it to the template via get_context_data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question