将单个应用程序 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/