15

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.

Duck
  • 178
  • 1
  • 11
yoK0
  • 325
  • 1
  • 3
  • 17

5 Answers5

13

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.

AAA
  • 1,962
  • 4
  • 30
  • 55
11

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.

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42
  • Thank you. It seems like the most correct answer. So I was pretty much on the subject with view returning other views. Is this how real developers do things or its just the answer to my dumb question ? – yoK0 Jul 09 '14 at 04:51
  • The projects I have looked at are set up that way. Look at the Mezzanine project- follow the instructions to set up a project, and then look at the repository. It will set you up with a project level app that leverages the various apps required to create a unified experience. – Paul Becotte Jul 09 '14 at 12:37
3

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.

Jon
  • 66
  • 4
  • I know what you mean but this isnt the right solution to achieve what I want. I read somwhere this solution; create views like always then create view that renders that other views. I could create view home that renders all of the apps I want on page and I could still access those apps separetly. Anyway thanks for your answer. – yoK0 Jul 08 '14 at 05:27
1

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

Community
  • 1
  • 1
Carlos Castellanos
  • 2,358
  • 4
  • 25
  • 43
0

In my experience this is generally done with template tags https://docs.djangoproject.com/en/dev/ref/templates/builtins/. In your example to add a poll to a page you would create a polls application then using a template tag add the poll to the page. The poll would not be part of your view.

mrfunyon
  • 99
  • 1
  • 4
  • Ok so what would happened if you want to have on you page something more than poll ? You could create view that returns poll and you other app, but you wont be able to use that other app alone in your other project. – yoK0 Jul 09 '14 at 01:55
  • You might be over thinking this. Your view should only contain information about a single model it should not contain anything else. Template tags are helpers for your views to inject additional information. – mrfunyon Jul 09 '14 at 02:29
  • I agree. But if view wont return that other app you want then you will not be able to use it with template tags. And we're back to the main question. I dont undarstand, is it some magic stuff I ask for? Just want to know how buil regular websites with django. – yoK0 Jul 09 '14 at 05:09
  • @yoKO yes, it does bind your templates to other apps but the apps themselves are all decoupled. The view layer is where apps generally become coupled with your instance of the application. – mrfunyon Jul 09 '14 at 06:05