Question

is there the possibility to create an django app where the user can create pages and subpages. E.g. he creates a page SEO and a subpage Google SEO:

  • SEO
    • Google SEO
    • Yahoo SEO

I'm not talking about the default cms pages from Django and not about a menu structure like this, but about the layout of the backend. It's important that the user can create the pages and subpages within one app.

A tutorial or additional infos would be great.

Thanks

PS: Are sub-applications what I am looking for?

How do I create sub-applications in Django?
Django sub-applications & module structure

Was it helpful?

Solution

The app below lets you store simple “flat” HTML content in a database and handles the management for you via Django’s admin interface .

A flatpage can use a custom template or a default.

Models.py

here       = lambda x: os.path.join(os.path.abspath(os.path.dirname(__file__)), x) 
templates  = os.listdir( here("templates/pages/") ) 

class Categorie(models.Model):
    nom  = models.CharField(max_length=250)
    slug = AutoSlugField(populate_from='nom', unique=True)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', verbose_name=u"Parent category")

        def get_pages(self):
            pages = Page.objects.all().filter(categorie__slug=self.slug)
            return pages


class Page(models.Model):
    url                   = models.CharField(_('URL'), max_length=100, db_index=True)
    title                 = models.CharField(_('titre'), max_length=200) 
    categorie             = models.ForeignKey(Categorie, null=True, blank=True)
    template_name         = models.CharField(_('template name'),choices=[ ( str(templates[i]), str('pages/'+templates[i])) for i in range(len(templates)) ], max_length=70, blank=True,help_text=_("Example: 'pages/contact_page.html'. If this isn't provided, the system will use 'pages/default.html'."))
    ordre                 = models.IntegerField(blank=True, null=True,default=0) 
    content               = models.TextField(blank=True)

Urls.py

   urlpatterns = patterns('django.contrib.yourappname.views',
    (r'^(?P<url>.*)$', 'page'),
  )

views.py

def page(request, url):
    if not url.endswith('/') and settings.APPEND_SLASH:
        return HttpResponseRedirect("%s/" % request.path)
    if not url.startswith('/'):
        url = "/" + url

    f = get_object_or_404(Page, url__exact=url)

    return render_page(request, f)


@csrf_protect
def render_page(request, f):


if f.registration_required and not request.user.is_authenticated():        
    return redirect_to_login(request.path)

if f.template_name:
    t = loader.select_template((f.get_template_name_display(), f.template_name))
else:
    t = loader.get_template(DEFAULT_TEMPLATE)

f.title = mark_safe(f.title)
f.content = mark_safe(f.content)

c = RequestContext(request, {
    'page': f
})
response = HttpResponse(t.render(c))
populate_xheaders(request, response, Page, f.id)
return response

Now you can use your a tag to display the pages in a menu structure based on categories

@register.inclusion_tag('pages/tags/menu.html')
def get_pagestatiques_structured(cat=None, template_name='pages/tags/menu.html'):    
    category = Categorie.objects.all().get(slug=cat)
    subcategories = Categorie.objects.all().filter(parent=category.id)
    return locals()

P.S: this app is based on flatpage app

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top