django中如何共用数据库与不同app绑定不同域名?

我有个网站要给做个手机站点,数据库肯定是用现在的app的,而且url的规则也是一样,但是模板不能一样但是问题来了:

因为手机域名是跟电脑是不一样的简单来说就是二级域名,比如电脑是这样www.baidu.com
那么手机站域名就是这样
m.Baidu.com

我的思路是这样的,建立一个新的app,然后url规则,和modles都引用电脑站,然后视图用自己的这样可以吗?
不过就算可以,那如何实战不同app绑定不同域名?

又或则我有一个奇葩的需求,就是不同的django工程用的是同一个数据库呢?

阅读 6.7k
2 个回答

既然你的URL都不变,那么你完全不用重新建一个app的,在原来的app上做自动选择模版,比较简单的,CBV就是让你使劲复用的啊。
来先定义一个BaseView:

class BaseView(View):
    @property
    def ismobile(self):
        # 开发的时候你可以直接返回True或者False来调整不同的模版
        return 'm.maidu.com' in self.request.get_host()
    
    def render(self, request, templ_name, context):
        if self.ismobile:
            templ_name = 'mobile/%s'%templ_name
        return render(request, templ_name, context)

后面的其他View都都从这里继承,渲染模版直接用self.render就行了,其它东西都不用变,在templates下面新建mobile文件夹,再重写一套模版就行了。

我做过类似的事情供你参考,假设项目名称为testsite,app名称为news。

1. 在news app中除了urls.py外,还建另外一个mobileurls.py

在mobileurls.py中的url pattern可以和urls.py中一样,但View不一样。
在不同的view中使用mobile的模板做渲染。
实例代码如下:

  # urls.py
  from django.conf.urls import url
  from .view import pc_news

  urlpatterns = [
      url(r'news', pc_news),
  ]

  # mobileurls.py
  from django.conf.urls import url
  from .view import mobile_news

  urlpatterns = [
      url(r'news', mobile_news),
  ]

2. 在project的urls.py中,为app中两个urls.py添加不同的前缀

  # urls.py
  from django.conf.urls import include, url

  urlpatterns = [
      url(r'^pc/', include('news.urls'),
      url(r'^mobile/', include('news.mobileurls'),
  ]

3. 配置不同的域名对应不同的url
当时我的部署环境是nginx + uwsgi,因此只要配置nginx的配置文件就可以了。

  # nginx.conf
    user              nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log;

    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  pc.news.com;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location ~ ^/pc/ {
                include     uwsgi_params;
                uwsgi_pass  unix:/var/run/django.socket;
                uwsgi_param UWSGI_SCRIPT    testsite.wsgi;
                uwsgi_param UWSGI_CHDIR     /opt/testsite/;
                index       index.html index.html;
                client_max_body_size        35M;
            }
        }
        server {
            listen       80;
            server_name  mobile.news.com;

            location ~ ^/mobile/ {
                include     uwsgi_params;
                uwsgi_pass  unix:/var/run/django.socket;
                uwsgi_param UWSGI_SCRIPT    testsite.wsgi;
                uwsgi_param UWSGI_CHDIR     /opt/testsite/;
                index       index.html index.html;
                client_max_body_size        35M;
            }
        }
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题