Django 通用模板

发布于 2024-08-09 09:37:15 字数 196 浏览 3 评论 0原文

所以,通用视图非常酷,但我感兴趣的是通用模板。

例如,我可以给它一个对象,它会为我串起来。

或者如果我给它一个列表,它只会迭代对象并将它们作为 ul (或 tr,或它认为必要的其他任何内容)字符串。

对于大多数用途,你不需要这个。我只是为一个朋友快速拼凑了一些东西(如果你一定知道的话,一个酒吧股票应用程序),而且我不想编写模板。

So, Generic views are pretty cool, but what I'm interested in is something that's a generic template.

so for example, I can give it an object and it'll just tostring it for me.

or if I give it a list, it'll just iterate over the objects and tostring them as a ul (or tr, or whatever else it deems necessary).

for most uses you wouldn't need this. I just threw something together quickly for a friend (a bar stock app, if you must know), and I don't feel like writing templates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-08-16 09:37:15

如果有适用的 django 模型,您只需使用 django.contrib.admin 或 django.contrib.databrowse 即可。如果没有,那么您可以通过完全跳过 django 模板来进行管理。示例:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

但是您当然想避免写那么多,因此我们可以使用纯文本和 pprint 模块来代替 html:

from django.http import HttpResponse
import datetime
from pprint import pformat

def current_datetime(request):
    now = datetime.datetime.now()
    return HttpResponse(pformat(now), mimetype="text/plain")

编辑: 嗯...这似乎是视图装饰器应该处理的事情:

from django.http import HttpResponse
import datetime
import pprint

def prettyprint(fun):
    return lambda request:HttpResponse(
            pprint.pformat(fun(request)), mimetype="text/plain")

@prettyprint
def current_datetime(request):
    return datetime.datetime.now()

If there's a django model for it, you can just stick to django.contrib.admin or django.contrib.databrowse. If not, then you might manage by skipping the django template altogether. example:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

But of course you wanted to avoid even writing that much, so instead of doing html, we can use plain text and the pprint module:

from django.http import HttpResponse
import datetime
from pprint import pformat

def current_datetime(request):
    now = datetime.datetime.now()
    return HttpResponse(pformat(now), mimetype="text/plain")

edit: Hmm... this seems like something a view decorator should handle:

from django.http import HttpResponse
import datetime
import pprint

def prettyprint(fun):
    return lambda request:HttpResponse(
            pprint.pformat(fun(request)), mimetype="text/plain")

@prettyprint
def current_datetime(request):
    return datetime.datetime.now()
始终不够 2024-08-16 09:37:15

我不认为你会放弃编写模板,特别是如果你想格式化它,即使是轻微的格式化。

但是,您可以重复使用基本模板,例如,创建通用 object_list.html 和 object_detail.html,

它们基本上包含循环对象列表并呈现它的信息,并显示对象详细信息。如果需要,您可以在整个应用程序中使用这些“通用”模板。

I don't see you getting away from writing templates, especially if you would want to format it, even slightly.

However you can re-use basic templates, for e.g, create a generic object_list.html and object_detail.html

that will basically contain the information to loop over the object list and present it, and show the object detail. You could use these "Generic" templates across the entire app if need be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文