English 中文(简体)
避免与Django站点框架的移动/台站的多余代码
原标题:Avoiding redundant code in mobile/desktop with Django sites framework

根据Django docs,提供移动式和桌面应用的最佳做法似乎是:

views.py

from django.contrib.sites.models import Site

def my_view(request):
    current_site = Site.objects.get_current()
    if current_site.domain ==  foo.com :
        # Render desktop home page
    elif current_site.domain ==  m.foo.com :
        # Render mobile home page

不幸的是,这意味着我每想一想一想就作出选择。 我可以做以下工作:

views.py

from django.contrib.sites.models import Site

current_site = Site.objects.get_current()
if current_site.domain ==  foo.com :
    def my_view(request):
        # Render desktop home page
elif current_site.domain ==  m.foo.com :
    def my_view(request):
        # Render mobile home page

在我开始通过我的看法而蒙上阴影之前,我想在此谈谈可能性。 试图以硬性方式检验这一点。

问题回答

http://pypi.python.org/pypi/django-mobility"rel=“nofollow”http://pypi.python.org/pypi/django-mobility?

Using a middleware for detecting the device and decorators to switch templates depending on the incoming device are a good approach to avoid redundant if/else constructs.

如果你看一看所举的“jan”行动的例子,那么他们就看上去了你所期望的建筑:

def view(request):
    ...

@mobilized(view)
def view(request):
   ...

You can use middleware to detect whether or not the request is to the m subdomain or not, and then specify the correct URL conf to direct you to the views you want. I ve been using a modified version of the django-subdomains app for this and it s been working nicely. This is an effective and simple solution if your view logic for your mobile site is quite different from the view logic of your regular site. Here s the link:

https://github.com/tkaeming/django-subdomains

那么,你们都必须做的是,为你们的移动地点写新的URL座椅,在你们的环境下具体说明这一点,然后就象你们经常想的那样,为你们的移动地点写你的见解/影子。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签