How can two apps respond to the same URL in Django? - django

I think I'm missing a basic concept here. In the stereotypical Django project, you'd have two apps responding to different urls:
http://localhost/myproj/app1/33
http://localhost/myproj/app2/newcomment.html
But what mechanisms exist to handle cases where the two apps are complementary - say one provides content, and the other provides presentation? Or maybe one is content and the other is a kind of static, side-wide content that should appear on every page.
In particular, I don't understand how both apps can use template inheritance to extend the same template. Imagine there's a base app "baseapp" with a template "base.html":
...
<div blah blah>
{% block content %}
{% endblock %}
...
App1 extends it:
{% extends "baseapp/templates/base.html" %}
{% block content %}
... here's the actual content...
{% endblock %}
App2 adds a little banner or something:
{% extends "baseapp/templates/base.html" %}
{% block content %}
<div class="banner">Please support our site!</div>
{{ block.super }}
{% endblock %}
So what are the ways that both templates can get displayed? I can think of:
app1 could extend app2's templates. But this seems wrong: app1 is the content provider, and shouldn't be dependent on something as trivial as app2.
app2 could extend app1's templates. But this seems wrong: now the URL scheme would have to funnel every URL through app2 (if I understand correctly)
middleware?
As I said, I'm probably missing something very basic. Or I'm making some very faulty assumptions that I don't know about. (This question is my third attempt, after Embed an optional Django application in another page, if that app is present and How to capture and display information external to my webapp, but relevant to users of it? - I'm having trouble framing the issue.)

App doesn't respond to an URL, a view does. View is a function that can use models, forms and other object from any app. There isn't any problem here.
If you want to add something to template, inheritance isn't the only way. You'd better use custom context processor or custom template tag.

I think what I was actually missing here:
Apps can override templates just by including a template of the right name in the right subdirectory. The Django docs don't make this very clear, that I can see: they refer to this functionality in the context of Admin templates
When overriding a template as above, you can't extend it, but:
This snippet lets you both override a template and extend it: http://djangosnippets.org/snippets/1376/
Here's a closely related question: Django: Overriding AND extending an app template

Related

Django Combine two templated from different apps

I have two apps with the following structure
project->
main app->
templates->
dashboard.html
my app->
templates->
mydashboard.html
I want to include mydashboard into dashboard, well this is possible using include template tag. but my problem appears when I have to pass some parameters into mydashboard, let's say they are named param1 and param2 these parameters are the variables which I have to load from my app app. well one possible method is to filling these params in dashboardview in mainapp and pass them using include tag into mydashboard.html like below
def user_dashboard(request):
...-->here I have to get data from my app (this view is in main app and I do not want to make main app be dependent to my app
return render(request, 'dashboard.html', {'param1': 0, 'param2': 34})
then in dashboard.html I add this part
{% is_app_installed "myapp" as is_myapp_installed %}
{% if is_myapp_installed %}
{% include "myappdashboard.html" with param1=param1 param2=param2 %}
{% endif %}
It seems the above method works but the main problem is that using this method mainapp is dependent to myapp and I do not want to this happens.
is there any other method to load those param1 and param2 inside myapp?
thanks
I can't see the details of the templates, but if you want to decouple the two templates I suggest you to rethink how you organise your templates logic. Try to rearrange the logic adding a base_dashboard.html in a common template dir with snippets and base templates to share between the apps.
Then the myappdashboard.html and dashboard.html can extend it with {% extend base_dashboard.html %}.

Django templates inheritance between apps

In Django templates there is extends tag used like {% extends "base.html" %}.
I want if possible to name templates in several apps the same (like templates/bookkeeping/bookkeeping.html).
I want the more specialized app (which handles bookkeeping for a particular project) to extend a template in a less specialized app (which handles common tasks for bookkeeping for several our projects) with extends. Particularly in the less specialized app I create a template without much design. In the more specialized app I want to present the same information but with a particular design (CSS, etc.)
What can be done about this in Django 1.11?
If it is impossible to use extends of a template with the same template name (but different app), what are other possible ways to solve the problem? I could check existence of derived.html and fall back to base.html if there is no derived.html, but this would lead to writing tedious Python code. Is there a better way?
Could you not just name the directories for the templates in each of the apps as following:
app1/templates/app1/bookkeeping/bookkeeping.html
app2/templates/app2/bookkeeping/bookkeeping.html
Then in the more specialised app (app2) extend by using:
{% extends "app1/bookkeeping/bookkeeping.html" %}
Or am I missing something here?
Edit:
You could link your Python code to point to one template, say templates/pages/bookeeping_base.html. In that template you could say:
{% if project.is_specialised %}
{% include 'app2/bookkeeping/bookkeeping.html' %}
{% else %}
{% include 'app1/bookkeeping/bookkeeping.html' %}
{% endif %}
Then as I described above, the app2 template could still extend the app1 template.

Django templates, pass template language through a template variable

I was thinking about putting my static pages in the database (simple model of url, title and content) and then having a basic base_static.html template - making them super easy to edit (from admin interface) if necessary.
I know you can easily escape html like so:
{{ content|safe }}
or
{% autoescape off %}
{{ content }}
{% endautoescape %}
But I need to be able to use some of the template language in the static pages, e.g. a simple for loop to go over variables passed to it from the view.
Is this possible, or would you recommend simply using templates to serve the static pages?(They won't change that much ever)
Thank you for your help.
If you want to store templates in db (and that is what you want if you want to use a template language) you can use this app : django-dbtemplates.
Check this question: Extending Django Flatpages to accept template tags
You may get some ideas from the solution exposed there

Django conditional template inheritance

I have template that displays object elements with hyperlinks to other parts of my site. I have another function that displays past versions of the same object. In this display, I don't want the hyperlinks.
I'm under the assumption that I can't dynamically switch off the hyperlinks, so I've included both versions in the same template. I use an if statement to either display the hyperlinked version or the plain text version. I prefer to keep them in the same template because if I need to change the format of one, it will be easy to apply it to the other right there.
The template extends framework.html. Framework has a breadcrumb system and it extends base.html. Base has a simple top menu system.
So here's my dilemma. When viewing the standard hyperlink data, I want to see the top menu and the breadcrumbs. But when viewing the past version plain text data, I only want the data, no menu, no breadcrumbs. I'm unsure if this is possible given my current design. I tried having framework inherit the primary template so that I could choose to call either framework (and display the breadcrumbs), or the template itself, thus skipping the breadcrumbs, but I want framework.html available for other templates as well. If framework.html extends a specific template, I lose the ability to display it in other templates.
I tried writing an if statement that would display a the top_menu block and the nav_menu block from base.html and framework.html respectively. This would overwrite their blocks and allow me to turn off those elements conditional on the if. Unfortunately, it doesn't appear to be conditional; if the block elements are in the template at all, surrounded by an if or not, I lose the menus.
I thought about using {% include %} to pick up the breadcrumbs and a split out top menu. In that case though, I'll have to include it all the time. No more inheritance. Is this the best option given my requirement?
You can put your hyperlinks inside a block that is overridden by the loading templates.
Let's say you have your framework.html like this:
{% extends "base.html" %}
<html>...<body>...
{% block hyperlinks %}
your hyperlinks here
{% endblock %}
rest of the code
</body></html>
You can then create something of a nolinks.html template and use it
{% extends "framework.html" %}
{# here you'll have everything from framework
but now we disable the breadcrumbs #}
{% block hyperlinks %}{% endblock %}
If you're getting the past data you can then use nolinks to render instead of framework.
I hope this helps.
From here: Any way to make {% extends '...' %} conditional? - Django
It can be done like this :
{% extends ajax|yesno:"ajax_base.html,main_base.html" %}
Or:
{% extends a_variable_containing_base_template_name %}
Which ever best suited for you.
Regards;

django: how to interconnect various logical pieces of a webpage

everyone. I'm just starting with django, so sorry for a possibly stupid question.
Imagine, I have a webpage with 3 logical pieces: content, dynamic menu, banners. It seems ok to me to have a template with something like:
{% include "banners.html" %}
{% include "menu.html" %}
{% include "content.html" %}
But then, how do I do it? Every piece has its separate logic in a separate view in its' separate app. How do I trigger execution of all three views and render all three templates?
P.S. Is there a sourceforge-like site for django apps where I could take a look at how people are doing stuff in real projects?
The standard way to do it is something as follows:
Templates:
Have a base.html that has the banners, menu and a body block which is empty.
For each template, extend the base.html and override the body block.
{% extends "base.html" %}
{% block body %}
-- Your this page's content goes here.
{% endblock %}
You can use the includes, where necessary, but prefer the extends where possible. Include was debated to be included in the template language.
Populating Context:
You now have a lot of templates with placeholders that need to be replaced with the real "context" values.
Pass the RequestContext, that should contain many standard requirements for the template.
For the values that you need in every template, write a template context processor.
Those context's that you need in this template, you populate in the view.
Using multiple app's views:
Most apps written for reuse will include template_name as a standard parameter, along with extra_context. You will need to call those views with these parameters.
Some apps go out of their way, to create a lazily evaluated response (like TemplateResponse) so that you can grab the context they populate, in your view.
Django reusable apps:
Are you kidding me? They are all over the Internet!
http://github.com/search?q=django&type=Everything
http://bitbucket.org/repo/all/?name=django
http://code.google.com/hosting/search?q=django&projectsearch=Search+projects