我想在 Django 上编写一个多表应用程序,所以我创建了两个数据库,其中一个默认使用,另一个 - "map"由特定应用程序使用 - "map"。
地图/models.py:
from django.db import models
class MapRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'map':
return 'map'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'map':
return 'map'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'map' or \
obj2._meta.app_label == 'map':
return True
return None
def allow_migrate(self, db, model):
if db == 'map':
return model._meta.app_label == 'map'
elif model._meta.app_label == 'map':
return False
return None
Run Code Online (Sandbox Code Playgroud)
设置.py:
DATABASES …Run Code Online (Sandbox Code Playgroud) def get(self , request , format=None):
body = request.data
name = body.get('name',"The_Flash")
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果 request.data 没有收到name值,我会硬编码值The_Flash,但我知道这不是一个好的方法。我希望将其作为变量添加到我的 django 项目的 settings.py 文件中。我浏览了像这样和其他一些人的参考资料,但这不是我想要的。有人可以告诉我哪种方法最有效。我正在使用 Django 1.8。
我的 urls.py 中有以下结构 - 它允许我在开发时直观地检查在浏览器中发送的电子邮件的格式:
urlpatterns = [Various url pattenrns]
if settings.DEBUG:
urlpatterns += [URL Pattern for checking emails]
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我运行测试套件时,代码仅检查settings.DEBUG一次 - 不是每次测试甚至TestCase运行时都检查。
我尝试在适用@override_settings于 urlpattern 的测试之前使用装饰器,DEBUG=True如下所示:
# Most of my tests run fine with Debug=False
@override_settings(DEBUG=True)
# Tests that use the URL pattern for checking emails
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法让它在测试之间正确切换 Url 模式...大概是因为我的 urls.py 文件仅在整个应用程序的测试中加载一次。
有没有办法在我的 urls.py 中使用这种类型的构造并运行我的测试?我是否有理由不应该在 urls.py 中使用这种类型的条件?
我想将 Django 数据库从 MySQL 迁移到 PostgreSQL。以前我使用过数据库配置选项,就像Django 网站上的示例一样。
数据库设置上的 MySQL 引擎有选项:“read_default_file”,因此我可以在外部文件上分离数据库凭据。
现在我准备使用 psycopg2 引擎切换到 PostgreSQL,但我找不到像 read_default_file 这样的类似选项。
有什么解决办法吗?
或者我可能应该更改为具有此选项的 PostgreSQL 的其他数据库引擎?谢谢
我正在尝试在 django 中调用环境特定设置。
我发现你可以根据以下内容在 django admin 中执行一些关闭操作:https ://docs.djangoproject.com/en/2.0/topics/settings/#the-django-admin-utility
我用manage.py尝试过:
python3 manage.py runserver --settings=mysite.settings.prod_settings
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ModuleNotFoundError:没有名为“mysite.settings.prod_settings”的模块;“mysite.settings”不是一个包
如何调用环境特定设置?
谢谢
我在我的 settings.py 文件中将 debug 设置为 False(请注意,在我将其设置为 false 之前,一切都完美无缺),当我运行git push heroku master并访问我的网站时,主页正在运行,但并非所有人都如此我的页面:在某些页面上,我收到一条错误消息Server Error (500)
这是我的settings.py代码:
DEBUG = False
ALLOWED_HOSTS = ["hacka-labs.herokuapp.com"]
Run Code Online (Sandbox Code Playgroud)
我注意到的是,我传递 id 的网址不起作用
如果你知道答案,请帮助我
我在使用 Celery、Redis 和 Django 时遇到问题。
我正在尝试使用它们来创建一个简单的任务。
但是,任务执行后不久就会出现错误。
我将在下面指定一部分代码以便更好地理解。感谢您的关注。
CELERY_BROKER_URL = 'redis://:password@REDIS:6379/0'
CELERY_RESULT_BACKEND = 'redis://REDIS:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Recife'
CELERY_BEAT_SCHEDULE = {
'task-send': {
'task': 'app.tasks.task_send_email',
'schedule': crontab(hour=5, minute=44)
}
}
Run Code Online (Sandbox Code Playgroud)
控制台芹菜
[config]
app: sistema:0x7fa254a5d6f4
transport: redis://:**@redis:6379/0
results: redis://redis:6379/0
concurrency: 1 (prefork)
task events: OFF (enable -E to monitor tasks in this worker)
[queues]
exchange=celery(direct) key=celery
[tasks]
app.tasks.task_send_email
INFO/MainProcess] Connected to redis://:**@redis:6379/0
INFO/MainProcess] mingle: searching for neighbors
INFO/MainProcess] mingle: all alone
Run Code Online (Sandbox Code Playgroud)
执行任务后出现错误
RuntimeWarning: …Run Code Online (Sandbox Code Playgroud) 我在分叉属于其他 Django 奥斯卡应用程序子集的 Django 奥斯卡应用程序时遇到问题。对于我正在关注的文档/教程,请查看此处。
我已经完成并成功分叉了未嵌套在其他应用程序中的应用程序,并相应地更新了我安装的应用程序。这是应用程序的子集,用于演示嵌套应用程序的一般模式
'dashboard_folder.dashboard.apps.DashboardConfig',
'oscar.apps.dashboard.reports.apps.ReportsDashboardConfig',
'oscar.apps.dashboard.offers.apps.OffersDashboardConfig',
Run Code Online (Sandbox Code Playgroud)
但是,文档(上面链接)没有明确概述如何调用嵌套应用程序。我认为它们可能会自动与它们的“父母”分叉,但是,一个简单的测试(下面的代码)证明并非如此:
如果我'oscar.apps.dashboard.reports.apps.ReportsDashboardConfig',改为
dashboard_folder.dashboard.reports.apps.ReportsDashboardConfig',
我收到以下错误:
ModuleNotFoundError: No module named 'dashboard_folder.dashboard.reports'
我想我会尝试一些关于如何调用嵌套应用程序来分叉它们的“有根据的”猜测,但是不幸的是,以下所有操作都失败了:
manage.py oscar_fork_app offers offers_folder
CommandError: There is no app with the label 'offers'
manage.py oscar_fork_app dashboard.offers offers_folder
CommandError: There is no app with the label 'dashboard.offers'
manage.py oscar_fork_app ReportsDashboard ReportsDashboard_folder
CommandError: There is no app with the label 'ReportsDashboard'
manage.py oscar_fork_app reportsdashboard ReportsDashboard_folder
CommandError: There is no app with the label 'reportsdashboard'
manage.py oscar_fork_app dashboard/reportsdashboard ReportsDashboard_folder
CommandError: …Run Code Online (Sandbox Code Playgroud) 我刚刚将 Django 项目从 3.1.6 更新到 3.2.3。运行后python manage.py runserver,服务器运行时出现以下警告:
core.CoreUser: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
HINT: Configure the DEFAULT_AUTO_FIELD setting or the CoreConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
Run Code Online (Sandbox Code Playgroud)
然后我记得在将项目从版本 < 3.2 更新到版本 >= 3.2 后需要添加一个DEFAULT_AUTO_FIELD设置,所以我这样做了:settings.py
core.CoreUser: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
HINT: Configure the DEFAULT_AUTO_FIELD setting or the CoreConfig.default_auto_field attribute to …Run Code Online (Sandbox Code Playgroud) 因此,当我尝试运行server或syncdb时遇到此CACHE错误.
这是追溯:https://gist.github.com/1538051
我尝试将其插入settings.py文件中:
CACHE_BACKEND = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
Run Code Online (Sandbox Code Playgroud)
但这给了另一个错误,这对我来说毫无意义.
if backend_uri.find(':') == -1:
AttributeError: 'dict' object has no attribute 'find'
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我解决问题是什么以及如何解决问题.
注意:我正在开发服务器上
django ×10
django-settings ×10
python ×4
celery ×1
database ×1
django-cache ×1
django-oscar ×1
django-urls ×1
heroku ×1
postgresql ×1
psycopg2 ×1
redis ×1
server-error ×1
task ×1