Login

Simple Flatpage Navigation Items

Author:
bradmontgomery
Posted:
June 12, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.