我是Python Django的新手。我正在使用Ubuntu,nginx和python,Django开发Django项目。
问题是: 我可以在浏览器中使用URL名称打开应用程序,如:http://example.com。但是应用程序不是在localhost:8000或public-ip:8000的服务器本地打开,下面是相关命令的输出:
$ wget -O- http://localhost:8000
--2017-01-17 13:15:30-- localhost:8000
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:8000... connected.
HTTP request sent, awaiting response... 404 NOT FOUND
2017-01-17 13:15:30 ERROR 404: NOT FOUND.
$ netstat -lan | grep 8000
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN
$ elinks http:// localhost:8000
Page not found at /
Page not found (404)
Request Method: GET
Request URL: http:// localhost:8000/
Raised by: mainpages.views.index
No tenant for hostname "localhost"
You're seeing this error because¤you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
请帮忙。
答案 0 :(得分:0)
根据docs for django-tennant-schemas
(我认为你正在使用它),它声明:
如果请求中的主机名与有效的租户domain_url不匹配,则将返回HTTP 404 Not Found。 )
因此,只需制作一个可用于本地主机的租户。
答案 1 :(得分:0)
这是因为您在应用程序中使用了tenant_schema。并且您在不创建租户的情况下运行应用程序。所以django说你先创建一个租户。
那是: 在使用
之后python manage.pt makemigrations and migrate
命令 将数据发布到您使用
创建的租户模型表中python manage.py shell
python shell会提示。这里导入您的租户模型类。并将数据传递给它。 前
python manage.py shell
>>> from xyz_app.models import Client
>>>
>>> Client(domain_url='energy.mystupidurl.com', schema_name='public', name='public', paid_until = '2099-12-31', on_trial =False).save()
此处客户是租户。和
domain_url,schema_name,pain_untill和on_trial
是类
的变量客户端
答案 2 :(得分:0)
这是配置共享和租户应用后执行的步骤:
SHARED_APPS = [
'tenant_schemas', # mandatory
'empresas', # you must list the app where your tenant model resides in
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.contenttypes',
'import_export',
'bootstrap4',
'usuarios',
]
TENANT_APPS = [
'app',
'productos',
'ventas',
'medidas',
# 'app.apps.AppConfig',
# 'usuarios.apps.UsuariosConfig',
# 'productos.apps.ProductosConfig',
# 'ventas.apps.VentasConfig',
# 'medidas.apps.MedidasConfig',
]
INSTALLED_APPS = list(SHARED_APPS) + [app for app in TENANT_APPS if app not in SHARED_APPS]
TENANT_MODEL = "empresas.Empresa" # app.Model
TENANT_DOMAIN_MODEL = "empresas.Domain" # app.Model
SITE_ID = 1
from empresas.models import Empresa
tenant = Empresa(domain_url = 'kinetfood.local',
schema_name = 'public',
nombre = 'Kirios Net',
nro_mesas = '4',
)
tenant.save()
tenant = Empresa(domain_url = 'titu.kinetfood.local',
schema_name = 'titu',
nombre = 'Titu Cocktail Xpress',
nro_mesas = '4',
)
tenant.save()
tenant = Empresa(domain_url = 'lasalva.kinetfood.local',
schema_name = 'lasalva',
nombre = 'La Salva burguer',
nro_mesas = '10',
)
tenant.save()
复制文件kinetfood / urls.py并将副本重命名为:public_urls.py
添加到设置PUBLIC_SCHEMA_URL_CONF = 'kinetfoood.public_urls'
运行python manage.py makemigrations
运行python manage.py migrate_schemas --shared
运行python .\manage.py shell
并粘贴create_tenant脚本,双击Enter。
在Windows上的C:\ Windows \ System32 \ drivers \ etc上设置相同的主机目录
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 kinetfood.local
127.0.0.1 titu.kinetfood.local
127.0.0.1 lasalva.kinetfood.local
运行python .\manage.py runserver
在浏览器http://titu.kinetfood.local:8000/
中放置url域
Voilá
希望对您有帮助。