开发者

Minimal Url and view for static pages on django

开发者 https://www.devze.com 2023-03-10 13:40 出处:网络
The app I am creating has many static pages, just like the pages of a website which do not change for some time. Im my model I will have a title field and a text field. I am looking go a way to avoid

The app I am creating has many static pages, just like the pages of a website which do not change for some time. Im my model I will have a title field and a text field. I am looking go a way to avoid multiple views and multiple urls for each page. I tried using flatpages, but I was not able to get to work the context processors. For example, if a certain page has many grids. So how to write a single view and a url which will deliver all the pages, along开发者_如何学Python with the context processors.


If you are having problems with flatpages, it's not hard to write your own version!

models.py

from markdown import markdown

class CustomFlatPage(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    slug = models.SlugField(unique=True)

urls.py

url(r'^(?P<slug>[-\w]+)/$','myapp.views.customflatpage', name='customflatpage'),

views.py

from django.views.generic.simple import direct_to_template
def customflatpage(request,slug):
page = get_object_or_404(CustomFlatPage, slug=slug)
    return direct_to_template('path/flatpage_template.html',
        extra_context={
            "page":page,
        })

template (has access to request context etc.)

{{ page.title}}
{{ load markup }}
{{ page.body|markdown }}

Use TinyMCE on the body field in django-admin (if you are using it) or your own form if you want rich text.

0

精彩评论

暂无评论...
验证码 换一张
取 消