我有公共(public)网站和管理网站的搜索表单。但他们有不同的模板但相同的 View 。
是否可以在网址中传递模板名称或使用一些条件逻辑
我有这个观点
class Search(View):
form_class = SearchForm
#i want something like that
if url1 == /public/search
template_name = 'search1.html'
else
template_name = 'search2.html'
最佳答案
如果你有两个独立的 url,你可以通过 urlconf 将变量传递给你的 View ;
url(r'^public/search/$', MyView.as_view(template_name="search1.html"), name= 'public_search')
url(r'^private/search/$', MyView.as_view(template_name="search2.html"), name= 'private_search')
Arguments passed into as_view() will be assigned onto the instance that is used to service a request. Using the previous example, this means that every request on MyView is able to use self.size. Arguments must correspond to attributes that already exist on the class (return True on a hasattr check).
关于python - 如何更改不同网址的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15964200/