user1039384
user1039384

Reputation: 521

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

Upvotes: 6

Views: 2752

Answers (2)

balazs
balazs

Reputation: 5788

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.

Upvotes: 3

Pill
Pill

Reputation: 5415

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

Upvotes: 5

Related Questions