2016-01-06 46 views
3

我用django製作了網頁應用程序,它在桌面瀏覽器上看起來非常漂亮,但是在移動瀏覽器中看起來有些頁面看起來並不完美。 我的模板目錄是myproject/template/我想爲移動瀏覽器myproject/template_mobile/製作新的模板,並且每當網頁訪問槽通過移動它重定向到myproject/template_mobile/模板否則它去槽myproject/template/。有沒有什麼辦法可以讓它形成setting.pydecorates?我不想改變整個視圖和舊模板。Django WebApp for Mobile and Desktop

在此先感謝。

+2

你可以使用['媒體queries'(http://www.w3schools.com/css/css_rwd_mediaqueries.asp)在你的CSS文件,如果你不」 t想要完全不同的移動模板。如果你使用它們,你不需要單獨的移動模板。 – doru

+0

我想爲移動設備製作完全不同的模板。 – shashank

回答

4

終於我得到了解決方案: 我使用django-mobile爲此,根據要求更改settings.py併爲此做了middleware.py

settings.py:(first follow `django-mobile`) 

DESKTOP_TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates'),) 
MOBILE_TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates/mobile'),) 

然後進行middleware.py

middleware.py 
from django.conf import settings 

class MobileTemplatesMiddleware(object): 
    """Determines which set of templates to use for a mobile site""" 

    def process_request(self, request): 
     # sets are used here, you can use other logic if you have an older version of Python 
     MOBILE_SUBDOMAINS = set(['m', 'mobile']) 
     domain = set(request.META.get('HTTP_HOST', '').split('.')) 
     if request.flavour=='mobile': 
      settings.TEMPLATE_DIRS = settings.MOBILE_TEMPLATE_DIRS 
     else: 
      settings.TEMPLATE_DIRS = settings.DESKTOP_TEMPLATE_DIRS 
1

有幾種方法可以解決這個問題。首先是要找到一種方式來獲得內查看設備信息,然後

from django.shortcuts import render 

def foo_view(request): 
    data = Model.objects.all() 
    my_template = 'template.html' 
    """ 
    your logic for device_info 
    """ 
    if device_info == 'mobile' 
     my_template='mobile_template.html' 
    return render(request, my_template, {'data': data}) 

另一種方法是使用django-responsive在這種情況下,你可以有DEVICE_INFO內基本模板所以在這種情況下,你可以有:

{% if device_info.type == 'phone' %} 
    {% include 'templates/mobile_template.html' %} 
{% else %} 
    {% include 'templates/template.html' %} 
{% endif %} 

第三種方法是重寫你的HTML/CSS代碼,並使用像基金會或Twitter的引導部分響應CSS框架

希望它可以幫助

+0

戈蘭,我不想改變我的整體觀點和模板。我正在尋找一種模板目錄可以更改的方式,如果它是移動的,那麼它會轉到移動模板,否則會轉到正常的模板。 – shashank

+0

然後使用django響應式應用程序,並從您想要的位置包含模板{%include'special_dir/template_name.html'%},這正是您想要的。 – Goran