python - 拼凑 Django View

标签 python django

将单个应用程序 View 视为可以拼凑在一起形成更大网站的 HTML block 是一种好的做法吗?如果不是,假设每个项目都使用一组不同的模板,那么在项目之间重用应用程序 View 的最佳方法是什么?

最佳答案

使用 template_name kwarg 定义 View 的一般良好实践。这允许覆盖默认模板。这在通用 View 中很常见。

#my reusable view
def list_items(request, template_name="items.html"):
  items=Item.objects.all()
  return render_to_response(template_name,
    {'items': items},
    context_instance=RequestContext(request))

#some other view
from my.reusable.views import list_items

def list_special(request, template_name="spectial_items.html"):
  return list_items(request, template_name=template_name)

关于python - 拼凑 Django View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1975769/

相关文章:

python - 如何检查 request.GET var 是否为 None?

python - Django Admin 不保存保留在初始状态的预填充内联字段

python - Ubuntu 终端自动执行 python 脚本。怎么阻止呢?

python - R 的 Jupyter 笔记本出现 Anaconda 库错误

Django自定义字段: Only run to_python() on values from DB?

python - 为什么运行 celeryd 时会得到 "SyntaxError: Non-ASCII character '\xa 3'"?

python - django templatetag,获取与当前帖子的 taggit-tags 相关的帖子

Django x-www-form-urlencoded 请求

python - 有谁在 iOS 上成功安装了 Selenium 吗?

python - 使用 Python-Requests 发送带有 Django View 的请求