Coder Social home page Coder Social logo

django-fspages's Introduction

django-fspages

Django-fspages is a small application for serving django page templates from the filesystem or other compatible storage. It may be used as a replacement for django.contrib.flatpages, have i18n support, is decoupled from Django ORM.

Application provide a fspages.views.serve view, which does mapping between matched path string in URL and page in the storage.

Each template may be supplemented with a serialized metadata file which have a same name as the template plus the entension suffix. Metadata is loaded by calling metadata loader (JSON by default).

If internationalization is enabled in django settings, the view will at first look for the localized version of the template by prepending the path with locale name directory and fallback to default language if not translation is available.

Rationale

For a django-powered website with dozens of semi-static web pages, usage of flatpages may not always be a convenient, as several problem would arise soon:

  • flatpages does not support i18n content. You can not by default provide a version of page both in English and Deutsch languages.
  • flatpage content is a static HTML code, you can use django template language there, access template context, etc. You can't fill several template's placeholders without creating own flatpage object.
  • there are two separate paths of customizing the site appearance by the editors - by modifying templates (served from the file system by default), then editing HTML content of FlatPage object (via django admin interface).
  • version control of the FlatPage objects require additional integration with tools like django-reversion

Django-fspages tries to solve this problems by utilizing django storage api for storing pages objects and render them as the templates. Site editors can edit pages as regular files with their favorite editors and version control systems.

FAQ

Why not pre-render/serve static files?
Static site generators work fine for their tasks, django-fspages is for sites which need to have semi-static pages (i.e. content depends on request parameters such as client IP address).
Why not to use template loader?
Comparing with dumb template rendering, django-fspages support correct path mapping, HTML redirects, metadata processing, i18n, sitemaps.

Installation and usage

Add fspages to settings.INSTALLED_APPS

Add fspages.views.serve into your urlconf, example:

from django.core.files.storage import FileSystemStorage
from fspages.storage import FSPageStorage

fspages_storage = FSPageStorage(
  backend=FileSystemStorage(location='/path/to/pages/'))

i18n_storage = FSPageStorage(
  backend=FileSystemStorage(location='/another/path/to/pages/'),
  index_document='index.html',
  metadata_extension='.meta.json')

urlpatterns = patterns('',
    url(r'^pages/(?P<path>.*)$', 'fspages.views.serve', {
        'storage': fspages_storage,
        }, 'fspages'),
)

urlpatterns += i18n_patterns('',
    url(r'^i18n_pages/(?P<path>.*)$', 'fspages.views.serve', {
        'storage': i18n_storage,
        }, 'i18n_fspages')
)

View options

serve view have two required parameters, and few optional parameters:

path (required)
Path string matched in urlpattern, which to be mapped to page template.
storage (required)
fspages.storage.FSPageStorage instance

FSPageStorage constructor parameters

backend (required)
django storage object instance, usually FileSystemStorage. django-fspages automatically extends FileSystemStorage with additional required methods declared in fspages.storage.StorageMixin. For other storage classes, you would need to provide those mixins on your own.
index_document
When path points to the directory, view tries to open index_document instead. Default: index.html
metadata_extension
File extension for metadata files. Default: .meta.json
metadata_loader
Callable which reveive a string with metadata file contents and needs to return a python dictionary. Default: json loader
metadata_defaults
Default metadata values. Default fspages.views.METADATA_DEFAULTS

Metadata parameters

Each page may be supplied with a metadata file with additional configuration parameters:

status_code
HTTP status code to respond with. Default: 200
template_context
Django template context to use in addition to RequestContext. Default: empty dictionary.
content-type
Custom content type. Default: mimetypes guessed type.
encoding
Page file encoding. Default: utf-8.
redirect_path
Place an URL of a target page if you need to do HTTP redirect. Page file SHOULD be in place, it may be empty. Default: False

django-fspages's People

Contributors

vldmit avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.