简体   繁体   English

Django视图和模板的分离

[英]Separation of Django view and template

For example, I have 2 pages listing news, one for desktop, one for mobile, the urls are 'news/list' and 'news/mobile/list'. 例如,我有2页列出新闻,一页用于桌面,一页用于移动,URL为“新闻/列表”和“新闻/移动/列表”。 Assume the logic of getting news from model is same. 假设从模型获取新闻的逻辑是相同的。

Currently, I work in this way. 目前,我以这种方式工作。

def mobile_list(request):
    complexLogic()
    return render(request, 'app/mobile/list.html', context)

def list(request):
    complexLogic()
    return render(request, 'app/list.html', context)

I am thinking how to do something like the following. 我正在考虑如何执行以下操作。 Is it currently supported in Django? Django当前支持它吗?

views.py views.py

def list(request):
    complexLogic()
    return request,'success',context

urls.py: urls.py:

('news/list', 'app.views.list', {'success' : 'app/list.html'}),
('news/mobile/list', 'app.views.list', {'success' : 'app/mobile/list.html'})

Yes, it's supported. 是的,它受支持。 You can read about it here . 你可以在这里阅读。

Basically, you want to add a success parameter to your list function, and then use that value: 基本上,您希望将success参数添加到list函数,然后使用该值:

def list(request, success):
    complexLogic()
    return render(request, success, context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM