django-templates

Django TEMPLATE_CONTEXT_PROCESSORS are being called too many times

点点圈 提交于 2019-12-23 19:59:00
问题 I need show some statistic numbers in all pages, so I decided to use context processors. But I just figured out that my function is being called 2 to 7 times every page load. I am doing 4 queries inside the function, so I am getting a very bad performance. Every page load it can take up to 28 (4*7) queries... I would like to know why this is happening and what can I do to avoid it. settings.py TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context

How do I prevent Django from interpreting a block which contains curly quotes?

隐身守侯 提交于 2019-12-23 19:13:30
问题 I'm working on a website which uses Django templates, and I have to produce HTML which contains paired curly quotes. Is there any way to disable Django's tag processing for a block of code? Something like a literal block tag would be nice, so that: {% literal %} {% LITERAL {{ BRACES }} {% endliteral %} ... would produce {% LITERAL {{ BRACES }} in the output. 回答1: EDIT: Your syntax is currently impossible with the current lexer / parser system. Why? Basically the template system has a Lexer

How does template in django get the user object?

心不动则不痛 提交于 2019-12-23 17:42:36
问题 How does template get user object? In other words what process exactly during rendering passes user object to template? And what else is accessible in template? 回答1: Using the django.contrib.auth.context_processors.auth context processor, you can access the auth.User instance in your template. If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain these variables: user – An auth.User instance representing the currently logged-in user (or an AnonymousUser

django custom templatetag not getting request in context

孤街醉人 提交于 2019-12-23 16:43:02
问题 I am using django 1.4 and trying to convert the code described at the end of this article into a customtag. This means I need access to the is_secure and site_name values from the request. Here is my CONTEXT_PROCESSORS in settings.py: CONTEXT_PROCESSORS = ( 'django.core.context_processors.request', 'django.contrib.auth.context_processors.auth', ) Here is my template tag code: from django import template register = template.Library() @register.simple_tag(takes_context=True) def full_static_url

How do I disable Django's autoescape from a view?

青春壹個敷衍的年華 提交于 2019-12-23 15:19:28
问题 Django says there's 3 ways to turn off autoescape: Use |safe after the variable Use {% autoescape on %} and {% endautoescape %} within blocks Use a Context like context = Context({'message': message}, autoescape=False) (1) and (2) work fine. But I have the situation where I have templates to generate plain-text push notifications, and I have LOADS of templates to build and maintain. I could go through and put the {% autoescape on %} and {% endautoescape %} tags in all of them, but (3) should

Load custom template tag from another application?

佐手、 提交于 2019-12-23 15:07:51
问题 Is this possible in any way? My project folder structure looks like this: my_project: app1: templatetags: my_tags1.py (contains simple tags and inclusion tags) app2: templatetags: my_tags2.py (contains some simple tags) templates: app1: index.html app2: index.html The ideas is to use a tag from my_tags1.py on the template app2/index.html. I simply tried: {% load my_tags1 %} but that gives me an error. 回答1: Make sure the tag files have different names. 来源: https://stackoverflow.com/questions

Refresh template in Django

喜欢而已 提交于 2019-12-23 13:01:07
问题 I have a view like this: def form1(request): if request.method == 'POST': form = SyncJobForm(request.POST) if form.is_valid(): # do something in_progress = True return render_to_response('form1.html', {'in_progress': in_progress}) I would like to know how to set it to refresh the template after is done with the view process. Like rendering the page after its done: in_progress = True return render_to_response('form1.html', {'in_progress': in_progress}) # after its finished finished = True

Django not like statement

流过昼夜 提交于 2019-12-23 12:08:09
问题 how to use not like in django queries Model.objects.filter(keywords not like "null" or "undefined") select * from model where keywords not like "%undefined%" or keywords not like "%null%"; 回答1: use the exclude function and Q objects Model.objects.exclude(Q(keyword__contains='undefined') | Q(keyword__contains='null')) 来源: https://stackoverflow.com/questions/4193097/django-not-like-statement

Overriding the Django admin change_list_results.html only for some models

孤人 提交于 2019-12-23 10:53:17
问题 I want to override the templates/admin/change_list_results.html and templates/admin/change_list.html templates for just one of my models. How do I tell the admin to differentiate this model from all the others in my app and render a different change template than the default? If I just edit change_list_results.html then all the models in the admin views will reflect my changes. 回答1: You can, see documentation here. The short version is that you need a custom template at admin/your-app-name

Why doesn't Django produce locale files from template files in another directory?

ε祈祈猫儿з 提交于 2019-12-23 10:28:17
问题 Version info: Django version 1.3 pre-alpha SVN-13858 Ubuntu GNU/Linux 10.10 I'm totally new to i18n and l10n in Django and currently I'm trying to make my Django project available in Dutch (in addition to its default language: English). I tried to apply the instructions given at http://docs.djangoproject.com/en/dev/topics/i18n/translation/ and http://www.djangobook.com/en/2.0/chapter19/ but I had no success. I don't know if this is related to my directory structure and template files being in