wordpress django currenct_active_page - django

in wordpress your template automatically kicks out a '.current_page_item' on your menu.
I am wondering if there is a django way of doing this?

Well django is not wordpress and also not a cms, but it could be used as one.
In this case you have to do on your own, it will depends how you designed your templates?

There are many ways to do this and it depends on how you are doing your menus. I usually create my menu as a Django model. Then, in my template I compare the current path with the menu path. eg.
<ul class="menu">
{% for m in menuitems %}
<li{% if m.path == request.path }} class="current"{% endif %}>
{{ m.title }}
</li>
{% endfor %}
</ul>
Of cause, you would need to pass in menuitems into your view. To save adding in that into all my views, I usually create a template tag that fetches the menuitems variable for me.
So yes, completely possible... but it completely depends on how you decide to structure your menus and pages. Django is a web-framework, whereas Wordpress is a Blog engine.

Related

Detecting a URL in a Django wizard_form.html template

I have three SurveyWizardViews all of which use the same standard wizard_form.html which is located at templates/formtools/wizard/wizard_form.html as per the documentation
I have added some basic logic to this template which is designed to detect which page of the form the user is on so that I can include a non standard page/step, this is an image with a JS slider bar underneath. This all works perfectly.
{% if wizard.steps.current == '6' %}
<img src="{% static "survey/images/pathtwo/" %}{{display_image}}"/>
<section>
<span class="tooltip"></span>
<div id="slider"></div>
<span class="volume"></span>
</section>
{% endif %}
However I now want to have a slightly different experience for the user depending on which View/URL they are coming from.
Question Is it possible to detect which URL the view is currently using to look at the page? e.g.
{% if URL.current == 'www.mywebsite.com/experiment/surveyone/' %}
do X
{% if URL.current == 'www.mywebsite.com/experiment/surveytwo/' %}
do y
I have done some searching but Im not even sure what I'm searching for to be honest. Any help would be much appreciated.
You can use the request context variable. Something like:
{% if 'experiment/surveyone' in request.path %}
do this
{% endif %}
I prefer using in instead of == to ignore trailing and leading slashes. If you want the whole thing try the build_absolute_uri method. Also check what options does request offer to you (https://docs.djangoproject.com/en/dev/ref/request-response/#httprequest-objects).
Finally, don't forget to add django.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS (I think it is added by default).

Adding an if statement to django-cms template tag

I'm probably missing something very obvious but can't see anything in the documentation. I have a carousel with a and each will hold an image. However I've added 6 but I want to add an if statement so if an Image has not been added you don't see a blank space, where there is no content inside the .
Here is what i've tried so far:
{% if "Carousel 1" %}
<li>
{% placeholder "Carousel 1" %}
</li>
{% endif %}
Attempt 2:
{% placeholder "Carousel 1" as cara1 %}
{% if cara1 %}
<li>
{{ cara1 }}
</li>
{% endif %}
Not sure if there is something differnt i need to be doing for the django-cms template tags?
Any help would be much appreciated. Docs here - http://docs.django-cms.org/en/latest/advanced/templatetags.html#placeholder
Not to be rude, but your approach is way, way off :)
Placeholders hold Content Plugins. Content Plugins are responsible for how they render their contents.
My advice would be to create or find a carousel content type plugin. This plugin will hold multiple images or "CarouselImage" model instances that you can iterate over, and also specify a template with which to render itself.
In this template resides the conditional statement you're wanting to check for. Placeholders are just that - places held for content plugins.

Django. Cancel button for form in admin site

Is there any functionality in admin site that allow to implement for every add/change form cancel button, that redirects me to list of that form objects. I mean some general solution for any form.
Add admin/submit_line.html in your project's templates directory. Use the code from the default submit_line.html, and add your cancel button. You can link it to just "../" to make it always just go up one level. Then do any necessary CSS styling to make it look right.
Create a file: yourapp/templates/admin/submit_line.html
I use Bootstrap, but you can change this easily
{% extends "admin/submit_line.html" %}
{% block submit-row %}
{{ block.super }}
Cancel
{% endblock %}
Be sure that your application is above "admin" in INSTALLED_APPS.
Looks like this in German locale:
<input type="button" name="Cancel" value="Cancel">
You can this line anywhere inside admin/submit_line.html

Rendering Foreign Keys in templates grouped by origin

I have an Area model, and I have Service model. Area is a Foreignkey of Service.
I want a template which shows each Service grouped under its respective Area, i.e.
Area 1
- service a
- service b
Area 2
- etc.
I've passed in an object list of all services to service_list.html. I have a custom tag get_areas which returns the areas, on which I can create the Area divisions, and from which I can potentially pass an area name to a service filter. But since I can't filter (can I?) in {% for service in object_list %}, how can I filter the service list in each Area's section of the HTML?
Thanks so much in advance.
If you post your models I can give you the exact code, but in general something like this should work:
# Pass in 'areas' variable from view with all required areas
{% for area in areas %}
{{ area.name }}
{% for service in area.service_set.all %} #Gets all the services associated with an area
{{ service.name }}
{% endfor %}
{% endofor %}
Not sure you even need a custom tag, but maybe I just don't understand that part.
Take a look at the regroup template tag. It was built for exactly the same purpose
https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#regroup
One thing that I have realized is that once you get the point of displaying complex interrelated data in your templates, that it makes sense to transform the data into an appropriate object (generally a list of dictionaries) before passing them onto the template.
That way, you can test your information easier, and have a much easier time displaying it. (You'll have much powerful tools at your disposition in Python based Views than in Django Templating Language).
#Maz - thank you for that. I'm learning at the moment and need to look at service_set.
#arustgi - that worked perfectly. For the benefit of fellow novices stumbling over this, I pass in 'queryset': Service.objects.all() and use:
{% regroup object_list by area as area_list %}
{% for area in area_list %}
<h2 class="separate">{{ area.grouper }}</h2>
{% for service in area.list %}
<div class="column">
<h3>{{ service.title }}</h3>
{{ service.body }}
</div>
{% endfor %}
{% endfor %}
Concise, descriptive code. Many thanks, both of you

How to create a dynamic menu structure with Django?

I want a menu structure with a menu and a submenu, and depending on
what page is currently viewed, I want to highlight items in both menus. Are
there any modules or apps that provide this? If not, what would be the best way
to approach the problem?
Quick google search gives this:
http://code.google.com/p/django-treemenus/
http://code.google.com/p/django-menuse/
You can also create such simple menu manually, just pass to the template list of menu items, active menu and list of submenu items for the active menu and the active submenu item:
<ul>
{% for item in menu_items %}
<li>
{% if item.id == active_menu_item %}
<span class="active-menu-item">{{ item }}</span>
<ul>
{# Similar code for submenu items #}
</ul>
{% else %}
<a class="inactive-menu-item" href="{{ item.url }}">{{ item }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
According to djangopackages, the most popular menu application in 2018 is django-sitetree.
Item highlighting can be done with the second tool from the link above, django-activeurl.
For large sites, however, django-sitetree may have performance issues.
There is another way which is related to the hierarchical structure of menus: modified preorder tree traversal, which optimizes database queries for tree lookups.
Use django-mptt to store hierarchical data (such as menus with submenus to unlimited depth). Use django-mptt-admin to easily manage them in admin menu. contenttypes framework may be useful for more generic menus (this is not shown in django-mptt tutorial).