开发者

How to structure a website in Django?

I have a simple (I hope so) question about the structure of a typical Django website. I have to restructure my site using some "Django-powered" pages mixed to other typical static pages.

I'd like to have a few templates and a lot of pages using these templates.

My question is:

What is the best wa开发者_JAVA技巧y to provide different content for each page? What I'm thinking right now is having different pages in my URL conf using the same templates and modifying the content within the View. But it's so unnatural to "hard-code" my content within the View, I suppose.

Is there a way to define content areas in my template and, for example, edit them within the Admin panel?

There must be better ways, I think.

Thank you!

Edit Maybe Flatpages could help in my task. But does exist a way to add "content areas" tied to specific parts of the templates? I can fine only the main content area.


How are your "content areas" different than django's template blocks? You can define something like this in your layout file:

base.html
{% block overridable %}Default content{% endblock overridable %}

And in your templates, you extend it like this:

 view.html
 {% extends "base.html" %}
 {% block overridable %} Overrided by view {% endblock overridable %}


You can use generic views, and I would heartily encourage you to. They're great. Check out the django docs for generic views. But as for different pages requiring a different template, yeah, that's how websites work.

Django is not "file-light" - it works by combining lots of small files. You're quite right to be averse to hard-coding HTML in your view controller - that's generally bad practice. You're going to have a template for each view, but really, how many views do you have? Even in a large application, the number of views that can't be handled by either a built-in or custom-made generic view is going to be pretty low.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜