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

标签 python django

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

相关文章:

python - 导入时对象的名称

python - 'tensorflow' 没有属性 'to_int32'

python - Django: AttributeError: 'NoneType' 对象没有属性 'split'

django - Django 中的 Celery(RabbitMQ 与 Django 数据库)

python - 将 CSV 从 AWS S3 实例导入到 Numpy

javascript - 调整图像大小然后上传的图像上传小部件

python - 使用 Python,以整数形式获取当前月份的日期

django - TDD Django 测试似乎跳过了 View 代码的某些部分

python - 如何在 Django 中将 S3 文件附加到电子邮件

python - 当我执行 Trades Record.objects.create(**traderecord_data) 时,会引发 TypeError