gpt4 book ai didi

python - 如何更改不同网址的模板

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:26 26 4
gpt4 key购买 nike

我有公共(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com