django-templates

How to make an object slider in django?

青春壹個敷衍的年華 提交于 2019-12-09 07:13:20
问题 I have written a simple article publishing site in Django 1.8. Here is the model that I'd like to slide: class Article(models.Model): nid = models.IntegerField(default=0) headimage = ImageWithThumbsField(upload_to='images', blank=True, sizes=((200,200),(400,400))) title = models.CharField(max_length=100) author = models.CharField(max_length=100, blank=True) body = models.TextField() teaser = models.TextField('teaser', blank=True) created=models.DateTimeField(default=datetime.datetime.now) pub

Django-Admin: integrating custom forms

我们两清 提交于 2019-12-09 06:54:50
问题 I am trying to build a import form for a CSV file into the admin interface for a given model. the view can be reached from the change_list of the model, but it throws a template syntax error. What am I doing wrong? Do I have to create my own template or can I re-use the existing admin/change_form.html somehow and I just don't get it? Traceback Line 60 is highlighted. Template error: In template site-packages\django\contrib\admin\templates\admin\change_form.html, error at line 60 Caught

django: guidelines for speeding up template rendering performance

南笙酒味 提交于 2019-12-09 05:39:44
问题 How would I go about speeding up Django template rendering? My template takes about 1-2 seconds or so to render, after the view function fully computes whatever it needs to. I've already attempted to perform all database access in the view, such that the template only hits RAM and not the DB engine. I do have a lot of include s - could there be an issue there? 回答1: I just spent a good deal of time optimizing my django templating code. Below are optimization guidelines that worked for me, but

Django separate thousands in template

旧城冷巷雨未停 提交于 2019-12-09 05:37:01
问题 My model is returning a Decimal(1234567.50), I can't seem to display the Decimal with a thousands separator. Does Django have a way to do this? Do I need to create my own template filter? Thanks. 回答1: You can use the intcomma filter, but I don't think it will work on Decimal objects and it works on Decimal objects. You'll have to convert to float/string first. 回答2: One easy way is to import locale and do the formatting in your view function. Even easier to read this: http://docs.djangoproject

Django - Did you forget to register or load this tag?

回眸只為那壹抹淺笑 提交于 2019-12-09 04:41:21
问题 I've created a custom tag that I want to use, but Django can't seem to find it. My templatetags directory is set up like this: pygmentize.py from pygments import highlight from pygments.lexers import get_lexer_by_name from django import template from pygments.formatters.other import NullFormatter register = template.Library() @register.tag(name='code') def do_code(parser,token): code = token.split_contents()[-1] nodelist = parser.parse(('endcode',)) parser.delete_first_token() return CodeNode

How do you update a django template context variable after an AJAX call?

无人久伴 提交于 2019-12-09 04:18:20
问题 I have a table Product that shows information of a group of products. <table id="item_table" class="table table-sm table-hover table-bordered"> <thead class="thead-inverse"> <tr> <th colspan="2">Date</th> <th colspan="6">Product name</th> <th colspan="2">Category</th> <th colspan="2">Amount</th> </tr> </thead> <tbody> {% for item in product_list %} <tr> <td colspan="2">{{ item.date }}</td> <td id="item_name_format" colspan="6">{{ item.name }}</td> {% if item.category_id %} <td id="item_name

How to include the default TEMPLATE_CONTEXT_PROCESSORS in the new TEMPLATES setting in Django 1.10

北战南征 提交于 2019-12-08 21:28:44
问题 I'm upgrading a project to Django 1.10 and it has code like the following: from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP TEMPLATE_CONTEXT_PROCESSORS = TCP + ( 'django.template.context_processors.debug', 'django.template.context_processors.i18n', 'django.template.context_processors.media', 'django.template.context_processors.static', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context

Django Templates: Use different css for pages

不打扰是莪最后的温柔 提交于 2019-12-08 21:11:16
问题 New to Django, I want to use different css files for different pages - i.e. page1.css for page1.html, page2.css for page2.html. Is there a way to do this while still extending base.html? In base.html {% load staticfiles %} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>{% block title %}Default Title{% endblock %}</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <!--

Django: How can I get a block from a template?

自作多情 提交于 2019-12-08 19:25:22
问题 Suppose my template has in it something like {% block subject %}my subject{% endblock %} and I load this template with tmpl = loader.get_template('mytemplate.html') , how can I extract "my subject"? 回答1: Camilo's solution doesn't work when your template extends a base. I've modified it a bit to (hopefully) fix that problem: from django.template import Context from django.template.loader import get_template from django.template.loader_tags import BlockNode, ExtendsNode def _get_node(template,

{% cycle %} work around for nested for loops?

爱⌒轻易说出口 提交于 2019-12-08 18:41:58
问题 I ran into an interesting "oversight" in the Django {% cycle %} template tag. This has been listed as a bug, but I'm wondering if there is a workaround for it? {% for r1 in range_0_2 %} {% for r2 in range_0_3 %} {{ r1 }}-{{ r2 }}-{{ cycle 'even' 'odd' }} {% endfor %} {% endfor %} This yields: 0-0-even 0-1-odd 0-2-even 1-0-odd 1-1-even 1-2-odd It should yield: 0-0-even 0-1-odd 0-2-even 1-0-even 1-1-odd 1-2-even 回答1: I have noticed the same problem in my templates. You can use a workaround like