python - Django: How structure/use project with multiple apps -
i've read official django tutorial , many other questions here on stackoverflow.com, seems there no real answer question or don't it. unfortunately, not find appropriate tutorial more 1 app.
my problem: django project consists of multiple apps. don't how apps can work together.
example: let's build news website , news website has blog posts, polls , comments area etc. think make sense create app called polls
, app called blog
, app comments
.
so, when browse url.com/news/5
example, you'll see blog post id 5. or when browse url.com/polls/2
you'll see poll id 2. ok.
now, however, don't want separated. don't when want have parts of website on single webpage (like in of websites). means if browse url.com/news/5
should not display news poll (because poll belongs news page thematically) , @ bottom of page comments article.
how structure , "glue" together?
normally, when browse url.com/news/5
defined view function within blog
app run , provides httpresponse. solution, however, cannot have poll , comments @ same page. hope it's clear problem is.
what "best practice" display different apps on single webpage? or done different?
i happy if can give me link real-world example or nice tutorial covers issue.
using class-based generic views , providing context. here link documentation:
and here example code:
from django.views.generic import detailview books.models import publisher, book class publisherdetail(detailview): model = publisher def get_context_data(self, **kwargs): # call base implementation first context context = super(publisherdetail, self).get_context_data(**kwargs) # add in queryset of books context['book_list'] = book.objects.all() return context
as can see main model publisher , get_context_data can add context: context['book_list'] = book.objects.all()
retrieves books database. can combine multiple models this.
Comments
Post a Comment