如果有显着差异(不同的形式等),我会说去制作两个视图 - 完全消除模板中的 url 逻辑,也不需要真正的“测试” - 不必检查 request.path/pass url /ETC。
网址
urlpatterns = patterns('',
(r'^create/$', create),
(r'^edit$', edit),
)
意见
def create(request):
text = "Create something"
return render_to_response('same-template.html', {'text': text}, context_instance=RequestContext(request)
def edit(request):
text = "Edit something"
return render_to_response('same-template.html', {'text': text}, context_instance=RequestContext(request)
模板
{% text %}
也可以通过这种方式轻松地通过列表传递多个更改:
意见
def create(request):
data = []
data['text'] = "Create something"
data['form'] = CreateForm()
return render_to_response('same-template.html', {'data': data}, context_instance=RequestContext(request)
def edit(request):
data = []
data['text'] = "Edit something"
data['form'] = EditForm()
return render_to_response('same-template.html', {'data': data}, context_instance=RequestContext(request)
模板
{% data.text %}
{% data.form %}