How to create a dynamic menu structure with Django? - 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).

Related

Add button against each record on django admin change list page?

I want to add a button against each row on django admin change list page. When that button is clicked, i want to make entries in some database table(all this happens on the backend) and after that the button should be disabled on the same page against that row.
How can i achieve this? I have searched a lot button there is no solution available.
You are going to have to edit the changelist_result_list template at the point where the result items are generated (near the bottom) to add an element that provokes a request. For example:
<tr class="{% cycle 'row1' 'row2' %}">
{% for item in result %}
{{ item }}
<!-- NEW STUFF --->
{% if forloop.last %}
<td>{% if pk %}CLICK ME{% endif %}</td>
{% endif%}
{% endfor %}
</tr>
(Don't forget to also update the table headers further up)
You are going to have to provide a template context variable for {{pk}} by messing with the templatetag result_list in django.contrib.admin.template_tags.admin_list -- meaning, you are creating your own templatetag for this.
Then you need to set up a view and an url conf to handle the request the links send.
Let's say you're at foo/bar and click on the link for the row with pk=2. A GET request will be send for foo/bar/do_things_with_object_2.
Your url conf needs to capture the pk (and the model, here: bar) and your view needs to do whatever it is you want on the object.

django-cms menu not showing

I am trying to run a project in which header has some menu links which are not loaded. The code to load the menu is as follows
<ul>
{% load menu_tags %}
{% for child in children %}
<li><a {% if forloop.first %} class="first" {% endif %}href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title }}</a></li>
{% endfor %}
</ul>
It looks that the for loop is not running as the children array is empty. I am not getting the idea where to look for this children array. From where this array gets loaded. If django-cms query the database then what columns does it query.
The project was actually built on some older version of django 2.1. Now I am trying to run the project on django-cms 3.0.7. I am using mysql database and have imported the data.
As well as importing the data, you need to do some additional work.
See:
http://docs.django-cms.org/en/support-3.0.x/upgrade/2.1.html
http://docs.django-cms.org/en/support-3.0.x/upgrade/2.4.html
http://docs.django-cms.org/en/support-3.0.x/upgrade/3.0.html
There are some steps there that you can't leave out. In particular, see the notes in that second document about migrations, and about the cms moderator command.
Don't do anything without first making a database backup.

Django and tabbed navigationin templates

I am building a django project which among others has a customer model. Any customer can be a foreign key to some calendar entry models or some picture models. I want to be able to get in to a customers page (e.g domain/customoer/1) and be able to navigate to the different models the customer is related with (e.g all the pictures for the customer, all the calendar entries of the customer). I am using bootstrap for the "presentation of the site. My idea was to use tabs. So I created a pil for my main template the customer.html
<ul class="nav nav-pills">
<li class="active">Personal Data</li>
<li>History</li>
<li>Analysis</li>
<li>Diagnosis</li>
<li>Treatment</li>
<li>Appointments</li>
<li>Graphics</li>
</ul>
and i was thinking of including a template for each pill
<div class="tab-content">
<div id="personal-data" class="tab-pane active ">
<div id="history" class="tab-pane">History is in the making</div>
<div id="analysis" class="tab-pane">
{% include 'customer/analysis.html' with customer=customer %}
</div>
<div id="diagnosis" class="tab-pane">Diagnosis is differential</div>
<div id="treatment" class="tab-pane">Treatment what treatment??</div>
<div id="appointments" class="tab-pane">Ap point ment</div>
<div id="graphics" class="tab-pane">Now this is going to be hard!</div>
The templates in each tab can do different things like upload pics navigate to different pages etc.
When i hit on a pill the url (domain/customer/1/) won't change (naturally) to inform me to which tab i am at the moment. So my question is how can i know in which tab i am ath the moment? I want the user to be able to do different things from different tabs (upload pics etc) and I want to be able to redirect to the specific tab after the view is called? Maybe #id could be appended to the url to specify the tab, but it doesn't happen with bootstrap.
What is the best way to deal with tabs in django? Ajax maybe? Will I not have the same problem? Any ideas or lings would be nice too
I use template inheritance for this kind of thing. It's simple and flexible. For example, you can define your main navigation in your base template like so:
...
<li {% block news %}{% endblock %}>News</li>
<li {% block features %}{% endblock %}>Features</li>
<li {% block sport %}{% endblock %}>Sport</li>
...
Then, in your base templates for each of those apps you'd have something like:
<!-- news/base_news.html -->
{% extends 'base.html' %}
...
{% block news %}class="active"{% endblock %}
...

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.

wordpress django currenct_active_page

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.