Login

Simple Flatpage Navigation Items

Author:
bradmontgomery
Posted:
June 12, 2009
Language:
Python
Version:
1.0
Tags:
html navigation flatpage nav
Score:
1 (after 1 ratings)

Flatpages are great for simple html content. However, I wanted some way to associate a navigation menu (just a snippet of HTML) with one or more FlatPage objects. Additionally, I wanted to be able to edit these throught the Admin. This was my solution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# models.py

from django.db import models
from django.contrib.flatpages.models import FlatPage

class FlatpageNav(models.Model):
    '''
    A simple snippet of HTML that can be associated with multiple FlatPages.
    '''
    flatpages = models.ManyToManyField(FlatPage, \
        help_text='Select the Flatpages that should display this menu')
    name = models.CharField(max_length=255, \
        help_text='A name for this menu. This is not displayed on the FlatPage.')
    menu = models.TextField(help_text='Enter an HTML snippet for the menu.')

    def __unicode__(self):
        return u"%s"%self.name

# admin.py
from models import FlatpageNav
from django.contrib import admin

class FlatpageNavAdmin(admin.ModelAdmin):
    list_display = ('name', )

admin.site.register(FlatpageNav, FlatpageNavAdmin)

# Then, in your Flatpage default template, add the following:
    {% if flatpage.flatpagenav_set.count %}
        {% for nav in flatpage.flatpagenav_set.all %}
            {{ nav.menu|safe }}
        {% endfor %}
    {% else %}
         {# Include an alternative menu. #}
        {% include "flatpages/menu.html" %}
    {% endif %}

More like this

  1. Non-Javascript select list navigator by shacker 5 years, 11 months ago
  2. Restrict Flatpage To Group by nikolaj 6 years, 11 months ago
  3. Class-based view mixin for flatpages by schwuk 2 years ago
  4. Breadcrumbs for flatpages by jca 7 years, 2 months ago
  5. Row-Level, URL-based permissions for FlatPages by bradmontgomery 5 years, 8 months ago

Comments

Please login first before commenting.