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.
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.
- How to make Django Password Reset Email Beautiful HTML?
- Debug django app running inside docker image, using pycharm debugger
- How does Django serve media files?
- How to disable cache for development in django?
- How do I write a single-file Django application?
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.
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
- Getting Type error while opening an uploaded CSV File
- Django 3.1: StreamingHttpResponse with an async generator
- What does "Directory indexes are not allowed here." mean in a Django error?
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.