django-yasp

Yet Another Static Page Django app.


Keywords
django-yasp
License
MIT
Install
pip install django-yasp==0.5.0

Documentation

django-yasp

image

image

Another static page Django app.

Main features:

  • It does not use the sites app.
  • Allows grouping pages by a menu.
  • Optional template overriding by page.
  • Template tags to get a page or a group of pages by menu.
  • Page has an image field (optional).
  • Page itens can specify a link (redirect).
  • Pages can be orderable (if django-admin-sortable2 is installed).

Quickstart

Install django-yasp:

pip install django-yasp

Include it on INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'yasp',
]

Add to urls:

url(r'^', include('yasp.urls', namespace='yasp')),

Add to middlewares:

MIDDLEWARE_CLASSES = [
    ...
    'yasp.middleware.StaticPageFallbackMiddleware',
]

Features

Static pages in yasp are automatically routed to a slug that you specify when creating your page. Your static pages can be grouped in a Menu object. So your urls can be in the form menu-slug/page-slug or page-slug (pages without a relation to Menu).

To create links to static pages there are useful templatetags, as follows.

Note

All menus/pages that are used in a templatetag will be automatically created if they don't exist.

To load all pages inside a menu:

{% load yasp %}

{% get_pages_from_menu 'about-us' as pages %}

<ul>
  {% for page in pages %}
    <li><a href="{{page.get_absolute_url}}">{{page.title}}</a></li>
  {% endfor %}
</ul>

To get a specific page:

{% load yasp %}

{% get_page 'about-us/vision' as page %} {# Page 'vision' related to a menu 'about-us' #}
<a href="{{page.get_absolute_url}}">{{page.title}}</a>

{% get_page 'contact' as page %} {# Page without a menu. #}
<a href="{{page.get_absolute_url}}">{{page.title}}</a>

To get a URL to a specific page:

{% load yasp %}

<a href="{% get_page_url 'about-us/vision' %}">Our vision</a>

Custom templates

Static pages will be rendered using the yasp/default.html template by default.

You can customize the template used to render a page by placing a template with the same slug of the page, or directly on the template field on Admin.

Template path resolution order:

  • The path in the template field of your page, if provided.
  • yasp/{menu_slug}/{page_slug}.html
  • yasp/{page_slug}.html
  • yasp/default.html

Context of a static page template:

menu

The Menu object.

content

The FlatPage object.

object

Alias to content.

You can use a static page instance to link to an external page.

Example:

>>> from yasp.models import Menu, FlatPage
>>> menu = Menu.objects.create(name='About us', slug='about-us')
>>> page = FlatPage.objects.create(menu=menu, slug='google', link='http://google.com', title='Google')
>>> '<a href="{}">{}</a>'.format(page.get_absolute_url(), page.title)
'<a href="http://google.com">Google Inc.</a>'

>>> vision = FlatPage.objects.create(menu=menu, slug='vision', title='Vision')
>>> '<a href="{}">{}</a>'.format(vision.get_absolute_url(), vision.title)
'<a href="/about-us/vision">Vision</a>'

This construction is can be specially useful when you're build a navbar in templates:

{% load yasp %}
{% get_pages_from_menu 'about-us' as pages %}

<ul>
  {% for page in pages %}
    <li><a href="{{page.get_absolute_url}}">{{page.title}}</a></li>
  {% endfor %}
</ul>

Will render as:

<ul>
    <li><a href="http://google.com">Google</a></li>
    <li><a href="/about-us/vision">Vision</a></li>
</ul>

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ py.test