开发者

Static Text to Dynamic Text in Django Templating

Cheers all! I'm quite new to Django, but I've been digging around for few weeks now. I'm building a basic website around Django and I'm quite excited about the framework itself and its possibilities. My question may sound "noob" and if it does, please don't complain.

I've developed a news feed, pages, commenting, taxonomy and a bunch of other stuff which is now ready to use, but when I came to the point where I had to apply a template to my website, I suddenly thought that I missed something - static text.

You know all websites have some static text all over the place, like copyright notice, some slogan text, introductory text on the homepage, some text and/or images in the sidebars, which are static. I know that all of them could be broken down into models, but I wouldn't like the overhead of doing so on every little bit of textual/raw html data on my website.

I was thinking about using a single model which contained an html-block name and its contents, then selecting ones applicable to a certain view and passing the contents over to the template. So my question is - is that the right thing to do? Would I store the copyright notice at the bottom of my website in the database? I could use a single dictionary to store all the text-blocks available for convenient use in my templates, but what if their number grows very large? Would I have to query for only ones I need in each and every view of mine? Or should I introduce a special template tag to carry out this sort of work for me?

Just to give you an idea, here are the two options I have in mind:

# Option 1
{% load text %}
{% text some-text-label %}

# Option 2 (assuming text was passed in the Context)
{{ text.some-text-label %}

How exactly did you solve this sort of issue?

Issue? Yeah - you can tell me that I can hard-code my copyright notice in my templates and that's fine, but clients, clients, clients. They do not know HTML and they literally would like to edit any part of the website from inside the admin panel (in terms of content of cours开发者_如何学Ce). So yeah, hope that explains it ;)

Thanks for your answers!


You might want to check out one of the various third-party apps that do this sort of thing - django-flatblocks, django-chunks, generic-flatblocks.


If I am getting your requirements correct you can follow the below:

  1. First you'll have to create a base.html( or someothername) and create different blocks.
  2. You can create template tags and put your logic to fetch the data from the database(i.e. the copyright info and whatever you want)
  3. You can load the template tag to the respective blocks to get the data dynamically.
  4. Use this template as the base template and inherit it in all the other templates.

Hope it helps.


I've been using django-flatblocks (http://github.com/zerok/django-flatblocks) to handle this kind of information and keep it manageable via the admin.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜