Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple django apps in one view

I guess i have a simple question about better code organization.

Say i have multiple apps that also implement how these apps should be represented on presentation layer.

I am trying to understand how to organize the code if i need to present multiple apps on one page without using frames of course?

Quick example: say I have two apps (app1 and app2) both implmeneting their coresponding model and views. Now i need my index page to contain presentation of these two views. How can i implement the generic view that still utilizes the app views instead of going to their models directly? I would prefer my app to control its view still.

Thanks

like image 753
user1039384 Avatar asked Nov 16 '11 09:11

user1039384


2 Answers

I think you may use render_to_string shortcut Thus in app views you put:

render_to_string(somestuff) # instead of render() or render_to_responce()

and then in index view something like this:

render(request, 'index.html', {'block1': view1(request), 'block2': view2(request)})

PS: Also after i wrote this it doesn't look very nice (it looked better in my head :) ).

like image 176
Pill Avatar answered Oct 09 '22 11:10

Pill


Great question, I tell you how I did it.

I used (app specific) custom template tags. These template tags put extra stuff into the context, what you can use in your templates. You can read more from the docs about custom template tags.

There's a good book, Practical django Projects 2nd edition, which is a bit outdated I think, but gives you a great insight to project organization.

like image 41
balazs Avatar answered Oct 09 '22 11:10

balazs