Django -- Break up a request.path into each word? - django

I want to take the URL from request.path in a template and do a test on just the first word. If my URL is this:
/estimates/commercial
I want to somehow remove the commercial from the request.path. I was hoping this is possible within the template, too, since I need to do this check at every page.
Is this possible within my Django template?
EDIT - for clarification
The purpose of this is to highlight the navigation link based on what page is currently being viewed. I have a main nav and a sub nav and I want to highlight as follows:
main nav --> [Systems][Estimates]
"Systems" sub nav ---> [New][Details][Invoives]
If I am in the Details section of the Systems section, I want the words System and Details to be a different colour, or underlined, or something.

Your options are:
Write a ContextProcessor
Write a custom template filter. I'd reccomend just writing a split template filter and then using the join and slice filters to get the desired affect. However, it might be easier to just write a filter that does the whole thing.
Truthfully I'd consider #1 to be the better option. You are using context processors already right? If not, now is the time =)
def somename(request):
return { 'some_context_var': request.path.split('/')[:-1] }
Edit:
If your path was /something/foo/bar/estimates/commercial/
def somename(request):
primary, secondary = request.path.split('/')[-2:]
return { 'primary_name': primary, 'secondary_name': secondary }
would give you 2 context vars with 'estimates' and 'commercial' as their values. This idea is pretty easy to expand, or even make more abstract and allow an arbitrary number of context variables being added.

You could use django-treemenus, which can be extended in many ways. In the manual, there is also an example how to highlight a navigation item if the user is within a certain path.

Without writing your own custom template filter, it'd be easier, I think, to do this in the view code and pass the relevant portion into the template.

Related

Determining parent page of template

Inside a Grails GSP template (e.g., _form.gsp), how can I determine what page is calling the template (e.g., create.gsp or edit.gsp) so that different fields can be displayed?
There is no general way, no. If you just know that in your application that the foo action always renders the foo view and there aren't exceptions to that, then of course you can just look at the action name. You could also do something simple like put something in the model when rendering the template, like <g:render template="foo" model="[parentPage:'edit']"/>, or set a variable in pageScope or similar. There are ways to accomplish what you want, but no general way in a template to answer the question "What GSP rendered this template?". I hope that helps.
So I googled it without any mention of templates (gsp determine current page) and found the answer on this Nabble thread. I will use params.action to determine what action I'm currently doing (e.g., create or edit), and use that to display different fields.
Cheers!

Flexible block positioning in Django?

In Drupal you could choose in which "region" of your site you want your block displayed. You did not have to modify any php/html code in order to achieve this.
Can such a thing be achieved with Django, and if yes, how?
By block I understand a piece of html output that doesn't have it's own URL and gets displayed along side the main data. (for example a search box or a poll)
Hm you probably want to create context processor and just output from it where you want it in template?
If you want reordering of content blocks in html output inside admin then you need something to generate that output like cms. You could try something like django-fluent-contents for this without requiring big cms.
Django and Drupal shouldn't be compared like this: Drupal is a CMS, Django is a web framework.
If you want to get a somewhat similar experience, I would look at using django-cms. With this, you can create numerous templates and set placeholders within these templates (these are regions of the page like 'sidebar', 'footer', 'content area' etc.). When you go to create a new page in django-cms, you select which template you want to use (maybe a two column layout or a three column layout with a header - depending on what you have created) and then you choose what content (or plugins) you want to place within the placeholders you have created in the template. So this is a somewhat similar experience to Drupal's regions.

What is the purpose of django tempate tags in the MVC model?

It is my understanding that template tags are a way to organize presentation logic, is this correct?
What I want to do is load/read a bunch of my model objects, but show them in different areas of the template according to a filter.
The first thought was to implement a tag get_myobjects_by_filterx but then I have to implement it for each filter I have, and I would use it by calling the tag on the areas of my template where I want them to show up.
So the second thought was to move the filterx into the tag itself, so the call would be get_myobjects_by "X" (passing the filter as string) then in the tag filtering and returning. Is this the prefered way? What if I need to pass another object not just a string?
Could I use a filter instead?
Little more detailed what I want to do: Load a kind of my objects and filter it by some of its fields, depending on which filter is applied, show it in different areas of my template. For example I want "if objects filtered by X show all of those in the column x else in column y and so on".
How do I solve this kind of question with django?
Thanks.
As far as my understanding goes, template tags are nothing but some python functions which can be used inside the templates. Because a django template is not your normal python program.

Django - Static content display based on URL

I'm working on a Django site with a basic three column design. Left column navigation, center column content and right column URL specific content blocks.
My question is about the best method of controlling the URL specific content blocks in the right column.
I am thinking of something along the lines of the Flatpages app that will make the content available to the template context if the URL matches a pre-determined pattern (perhaps regex?).
Does anyone know if such an app already exists?
If not, I am looking for some advice about the best way to implement it. Particularly in relation to the matching of patterns to the current URL. Is there any good way to re-use parts of the Django URL dispatcher for this use?
Django CMS is a good suggestion, it depends on how deep you want to go. If this is just the beginning of different sorts of dynamic content you want then you should go that way for sure.
A simple one-off solution would be something like this:
You would just need to write a view and add some variables on the end of the URL that would define what showed up there. Depending on how fancy you need to get, you could just create a simple models, and just map the view to the model key
www.example.com/content/sidecontent/jokes/
so if "jokes" was your block of variable sidecontent (one of many in your sides model instances) the urls.py entry for that would be
(r'^content/sidecontent/(?P<side>)/$,sides.views.showsides),
and then in your sides app you have a view with a
def showsides(request, side):
Sides.objects.get(pk=side)
etc...
For something like this I personally would use Django CMS. It's like flatpages on steroids.
Django CMS has a concept of Pages, Templates, and Plugins. Each page has an associated template. Templates have placeholders where you can insert different plugins. Plugins are like mini-applications that can have dynamic model-based content.
Although Django-CMS is an interesting suggestion, there are quite a few projects that do specifically what you've requested - render blocks of content based on a URL. The main one that I know about is django-flatblocks.

Multiple views loaded into a template

Hay guys, in PHP when i want to load a differnet page (render, with all variables outputting) i would use
include 'mypage.php'
This would load the page up and render the output.
How do i do something like this in Django? My problem is that i have a "quick list" which lists a bunch of popular items. I want this list to appear on every page, even if i don't call the object.
any ideas?
EDIT: The view is called shop.app.popular_items
You can use Inclusion tags.
Use template context_processors
To answer your question to becomingGuru on when to use context processors and when to use inclusion tags, remember that context processors are run on every template render. So they should only be used when you know you will always need the value.
Edit:
After Daniel's answer and as you said you want to have it on every page,
becomingGuru's answer is probably the best solution.
This does not belong into the view (if you not want to display this as a standalone page).
This should go into a custom template tag.