Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Multiple apps on one webpage?

Tags:

python

django

I've looked all over the net and found no answer. I'm new to Django. I've done the official tutorial and read many more but unfortunately all of them focus on creating only one application. Since it's not common to have a page as a single app, I would like to ask some Django guru to explain how I can have multiple apps on a webpage. Say I go to mysite.com and I see a poll app displaying a poll, gallery app displaying some pics, news app displaying latest news etc, all accessed via one url. I know I do the displaying in template but obviously need to have access to data. Do I create the view to return multiple views? Any advice, links and examples much appreciated.

like image 453
yoK0 Avatar asked Jul 08 '14 02:07

yoK0


4 Answers

You could do something like this to display app data on a page.

views.py

def home(request, template='path/to/template'): 
     context = {
        'polls': Poll.objects.all(),
        'galleries': Gallery.objects.all(),
    }
    return (request, template, context)

In the template:

{% for poll in polls %}
{{ poll }}
{% endfor %}
{% for gallery in galleries %}
{{ gallery }}
{% endfor %} 

urls.py

url('home/$', app.views.home, name='home')

But if you want to display the information like on a sidebar where it will be displayed all the time, then you'd want to use template tags.

like image 82
AAA Avatar answered Oct 15 '22 20:10

AAA


A django app doesn't really map to a page, rather, it maps to a function. Apps would be for things like a "polls" app or a "news" app. Each app should have one main model with maybe a couple supporting ones. Like a news app could have a model for articles, with supporting models like authors and media.

If you wanted to display multiple, you would need an integration app. One way to do this is to have a "project" app next to your polls and news apps. The project app is for your specific website- it is the logic that is specific to this application. It would have your main urls.py, your base templat(s), things like that. If you needed information from multiple apps in one page, you have to have a view that returns info from multiple apps. Say, for example, that you have a view that returns the info for a news article, and one that returns info for a poll. You could have a view in your project app that calls those two view functions and sticks the returned data into a different template that has spots for both of them.

In this specific example, you could also have your polls app set up so that its return info could be embedded- and then embed the info into a news article. In this case you wouldn't really have to link the apps together at all as part of your development, it could be done as needed on the content creation end.

like image 28
Paul Becotte Avatar answered Oct 15 '22 18:10

Paul Becotte


Each page or view should be contained in a single app, however, you can load in other apps from within an app. This does however make that app dependent on the other apps being present.

So if you wanted to display something from an app called "otherapp" in this app called "thisapp" then in thisapp.views you would simply add an import to the top of the file like this

from otherapp.models import OtherAppModel

Once you've done the import you can then access that models fields and methods. Also make sure you've added all of the apps to the INSTALLED_APPS list in your settings file.

like image 3
Jon Avatar answered Oct 15 '22 18:10

Jon


You can create a layout with several divs, with the intention of distribute on each one everyone of your apps, you can easily draw inside each div the contents of every app url you want just like google search do with previews. On your specific case you might want to use common css for all your apps in order to avoid using css isolation techniques. Another solution is to use iframes.... This might be useful:

Ajax/jQuery - Load webpage content into a div on page load?

https://pythonhosted.org/django-portlets/

https://github.com/diefenbach/django-portlets

Hope it helps

like image 1
Carlos Castellanos Avatar answered Oct 15 '22 19:10

Carlos Castellanos