我的问题是如何在单个Django项目中提供多个urls.py(如urls1.py,urls2.py等)文件.
我使用的是Win7 x64,django 1.4.1,python 2.7.3以及服务器django dev-server工具.
我决定使用谷歌发现的一种方法
http://effbot.org/zone/django-multihost.htm
我创建了一个multihost.py文件并放入django中间件文件夹:
C:\python27\Lib\site-packages\django\middleware\multihost.py
Run Code Online (Sandbox Code Playgroud)
使用以下代码:
from django.conf import settings
from django.utils.cache import patch_vary_headers
class MultiHostMiddleware:
def process_request(self, request):
try:
host = request.META["HTTP_HOST"]
if host[-3:] == ":80":
host = host[:-3] # ignore default port number, if present
request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[host]
except KeyError:
pass # use default urlconf (settings.ROOT_URLCONF)
def process_response(self, request, response):
if getattr(request, "urlconf", None):
patch_vary_headers(response, ('Host',))
return response
Run Code Online (Sandbox Code Playgroud)
另外在我的项目setting.py文件中,我添加了一个映射字典,如上面显示的链接:
# File: settings.py
HOST_MIDDLEWARE_URLCONF_MAP = {
"mysite1.com": "urls1",
#"mysite2.com": "urls2"
}
Run Code Online (Sandbox Code Playgroud)
我还没有像上面的链接所描述的那样实现错误处理.
我的主机文件包括以下内容:
127.0.0.1 …Run Code Online (Sandbox Code Playgroud) 我在 Ubuntu 20.04 上安装适用于 python 的 mariadb 连接器时遇到了问题。\n我确实安装了适用于 C 的 mariadb 服务器和连接器,正如文档中所述:
\nsudo apt-get update \nsudo apt-get install mariadb-server\nsudo apt-get install libmariadb3\nsudo apt-get install libmariadb-dev\n \nRun Code Online (Sandbox Code Playgroud)\n还安装了:
\nsudo apt-get install libmariadbclient-dev\nRun Code Online (Sandbox Code Playgroud)\n然后:
\n$ pip3 install mariadb\nCollecting mariadb Using cached mariadb-1.0.3.tar.gz (66 kB) Building wheels for collected packages: mariadb Building wheel for mariadb (setup.py) ... error ERROR: Command errored out with exit status 1: command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-01wak5oz/mariadb/setup.py'"'"';\n__file__='"'"'/tmp/pip-install-01wak5oz/mariadb/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', …Run Code Online (Sandbox Code Playgroud)