我尝试在我的HTML模板上保持一个有点一致的命名方案.即index.html for main,delete.html for delete page等等.但是app_directories加载器总是似乎从按字母顺序排列的应用程序加载模板.
有没有办法总是先在调用应用程序的templates目录中检查匹配?
我的相关设置settings.py:
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.filesystem.load_template_source',
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
)
Run Code Online (Sandbox Code Playgroud)
我试过改变顺序TEMPLATE_LOADERS,没有成功.
按照Ashok的要求编辑:
每个应用程序的目录结构:
templates/
index.html
add.html
delete.html
create.html
models.py
test.py
admin.py
views.py
Run Code Online (Sandbox Code Playgroud)
在每个应用程序的views.py中:
def index(request):
# code...
return render_to_response('index.html', locals())
def add(request):
# code...
return render_to_response('add.html', locals())
def delete(request):
# code...
return render_to_response('delete.html', locals())
def update(request):
# code...
return render_to_response('update.html', locals())
Run Code Online (Sandbox Code Playgroud) 我正在运行Django 1.2开发服务器,每当我使用Chrome或Safari加载页面时,我都会收到这些Broken Pipe错误消息.我的同事在从他的开发服务器加载页面时也会收到错误.使用Opera或Firefox时,我们没有这些错误.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 281, in run self.finish_response()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 321, in finish_response self.write(data)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 417, in write self._write(data)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 300, in write self.flush()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 286, in flush self._sock.sendall(buffer)
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
谁能帮我吗?我为此疯狂!
我想告知用户instance在我的pre_save接收器功能中处理数据时是否出现故障.
是否可以ValidationError从接收器功能中提出自定义?如果没有,我将如何实施这样的事情?
根据Django文档中的这一部分,我应该{% blocktrans %}用于需要翻译多元化的情况.但是,通过以下示例,我能做的更方便吗?
{% blocktrans count video.views.count as views %}
The video has been viewed <span>{{ views }}</span> time
{% plural %}
The video has been viewed <span>{{ views }}</span> times
{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)
我试着做以下事情:
{% blocktrans %}time{% plural %}times{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)
但它扔了 TemplateSyntaxError: 'blocktrans' doesn't allow other block tags (seen u'plural') inside it
我最近将我的一个Django项目中的所有视图迁移到了基于类的新项目中.对于基于函数的经典Django视图,django.views.decorators.http.condition如果存在与您指定的条件匹配的缓存副本,则可以使用一个方便的装饰器来绕过整个视图处理.我在文档和源代码中到处搜索,但是对于新的基于类的视图找不到任何实现.
所以我的问题是:您如何建议我为基于类的视图实现条件视图处理?
我有一些周期性的任务,我用芹菜运行(由supervisord守护),但在尝试在主目录中创建一个目录为我为主管的进程设置的用户我得到了"权限被拒绝"错误.在查看正在os.environ运行的芹菜任务中的dict后,我注意到USERvar设置为'root',而不是我在我的supervisord配置中为celery设置的用户.
这就是我的/usr/local/etc/supervisord.conf样子:
[unix_http_server]
file=/tmp/supervisor.sock
chmod=0777
[supervisord]
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:celery]
command=/home/<USER>/.virtualenvs/sync/bin/celeryd --beat --loglevel=INFO
environment=PYTHONPATH=/home/<USER>/apps/sync
directory=/home/<USER>/apps/sync
user=<USER>
numprocs=1
stdout_logfile=/var/log/celeryd.log
stderr_logfile=/var/log/celeryd.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 30
Run Code Online (Sandbox Code Playgroud)
可能是什么导致了这个?任何帮助将非常感激!
我有两个独立的celeryd进程在我的服务器上运行,由...管理supervisor.它们被设置为侦听单独的队列:
[program:celeryd1]
command=/path/to/celeryd --pool=solo --queues=queue1
...
[program:celeryd2]
command=/path/to/celeryd --pool=solo --queues=queue2
...
Run Code Online (Sandbox Code Playgroud)
我的celeryconfig看起来像这样:
from celery.schedules import crontab
BROKER_URL = "amqp://guest:guest@localhost:5672//"
CELERY_DISABLE_RATE_LIMITS = True
CELERYD_CONCURRENCY = 1
CELERY_IGNORE_RESULT = True
CELERY_DEFAULT_QUEUE = 'default'
CELERY_QUEUES = {
'default': {
"exchange": "default",
"binding_key": "default",
},
'queue1': {
'exchange': 'queue1',
'routing_key': 'queue1',
},
'queue2': {
'exchange': 'queue2',
'routing_key': 'queue2',
},
}
CELERY_IMPORTS = ('tasks', )
CELERYBEAT_SCHEDULE = {
'first-queue': {
'task': 'tasks.sync',
'schedule': crontab(hour=02, minute=00),
'kwargs': {'client': 'client_1'},
'options': {'queue': 'queue1'},
},
'second-queue': …Run Code Online (Sandbox Code Playgroud) 我有一个带有多个ImageFields的Django模型,并使用callable来确定上传路径.我想在上传路径中包含原始上传字段的名称,在本例tiny中为small,medium或press.
我能想到的唯一方法是创建一个替换file.nameuuid 的pre_save接收器.然后upload_to callable通过比较来找到匹配filename.这样做不是一种不那么黑客的方式吗?
class SomeDjangoModel(models.Model):
IMAGE_SIZES = ('tiny', 'small', 'medium', 'press')
def image_path(self, filename):
""" Example return: [some-django-model]/[medium]/[product1].[jpg] """
size = None
for field_name in self.IMAGE_SIZES:
field_fn = getattr(getattr(self, field_name), 'name', '')
if field_fn == filename.rpartition('/')[2]:
size = field_name
break
return u'{}/{}/{}.{}'.format(
slugify(self._meta.verbose_name),
size or 'undetermined',
self.slug,
filename.rpartition('.')[2].lower(),
)
tiny = models.ImageField(upload_to=image_path, blank=True, null=True)
small = models.ImageField(upload_to=image_path, blank=True, null=True)
medium = models.ImageField(upload_to=image_path, blank=True, null=True)
press = models.ImageField(upload_to=image_path, …Run Code Online (Sandbox Code Playgroud)