Multiple django apps in one view
Asked Answered
P

2

6

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

Paleolithic answered 16/11, 2011 at 9:7 Comment(0)
S
5

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 :) ).

Submarginal answered 16/11, 2011 at 9:45 Comment(2)
Gentlemen, my apologies for the delay to thank you both balazs and Pill for solid suggestions. While I think both are solid I will go with Pill's answer as it seems to suffice my needs.Paleolithic
Also, to give you extra thoughts why I like this a bit over the previous answer: While main usage is as simple as to write a CSS that defines the location of block1 and block2 and then in template use the actual {{block1}} and {{block2}} under corresponding divs, the other value is that the app class can also use that method to render its own template if required that presents the info for that app only. So the logic will be tied to each representation that wants to decide where to show the app and app will be responsible for the actual representation of its data. Thanks againPaleolithic
P
3

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.

Pharisee answered 16/11, 2011 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.