0

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

Community
  • 1
  • 1
Alexander Scholz
  • 2,100
  • 1
  • 20
  • 35

1 Answers1

0

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

Armance
  • 5,350
  • 14
  • 57
  • 80
  • thank you, but I get `NameError: name 'templates' is not defined` – Alexander Scholz Oct 22 '13 at 12:26
  • sorry`here = lambda x: os.path.join(os.path.abspath(os.path.dirname(__file__)), x) templates = os.listdir( here("templates/pages/") ) ` – Armance Oct 22 '13 at 12:31
  • Yes it works, thank you very much. But I have one more question: the Categorie field is empty. Where are these categories coming from? – Alexander Scholz Oct 22 '13 at 13:13
  • normal,You can use them or not, it may be a way to structure your pages. – Armance Oct 22 '13 at 13:17
  • I installed django categories and created 2 categories but they are not in the dropdown of my app. Can you please help me with this problem, too – Alexander Scholz Oct 22 '13 at 13:29
  • what do you mean by "they are not in the dropdown of my app"? – Armance Oct 22 '13 at 13:30
  • in Models you created a field "categories". In the backend this option field has just one option: "-------" and no categories – Alexander Scholz Oct 22 '13 at 13:53
  • yes ,you need to create a ctegorie(s) in order to have it (them) in the dropdown so go to your admin/yourappname/categorie and add a few (you must have added it in admin.py of course) – Armance Oct 22 '13 at 14:06
  • Okay, I got it but how can I assign a new subpage to a parent page? – Alexander Scholz Oct 22 '13 at 16:01
  • ASSIGN A PAGE TO CATEGORY "PARENT" OR "LEVEL1" WHATEVER YOU CALL IT, THEN ASIGNE SEVRAL AGES TO CATEGORY "LEVEL2" ..THEN USE YOUR TAG TO GET THEM IN THE RIGHT ORDER..SECOND OPTION IS BETTER : `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")` THEN `subcategories = Categorie.objects.all().filter(parent=category.id)` – Armance Oct 22 '13 at 16:18