django-templates

Django - How to show messages under ajax function

家住魔仙堡 提交于 2019-12-04 17:42:00
问题 I am using AjaxForm plugin to submit my form without refresh. like: $('#my_form_id').ajaxForm(function(){ //something on success or fail }); this works correctly. when i click submit button it saves form datas without refresh. But before this ; i had django messages on my template file like: {% for message in messages %} <div id="notice" align="center"> {{ message }} </div> {% endfor %} what this code does is displaying notifications if post saved correctly or something failed. now; i can't

Django template counter in nested loops

微笑、不失礼 提交于 2019-12-04 17:40:45
问题 Hi I have a list of two dictionaries I am passing to a Django template: base_parts = [ {'important item': 43}, {'lesser item': 22, 'lesser item': 3, 'lesser item': 45} ] in my template I can do this: {% for base_part in base_parts %} {% for k, v in base_part.items %} {# ...do stuff #} {# I try to get a running total of items to use as an ID #} inner ID: {% forloop.counter0 %}< br/> outer ID: {% forloop.parentloop.counter0 %}< br/> {% endfor %} {% endfor %} As you can see, what I want is a

Infinite recursion while extending the admin's app change_form template

你离开我真会死。 提交于 2019-12-04 17:21:08
问题 I have the following template in template/admin/change_form.html : {% extends "admin/change_form.html" %} {% block extrahead %} {% include "dojango/base.html" %} {% block dojango_content %} {% endblock %} {% endblock %} However for some reason it throws a TemplatesyntaxError: TemplateSyntaxError at /admin/cms/post/add/ Caught RuntimeError while rendering: maximum recursion depth exceeded while calling a Python object 回答1: I know it's late, but... If extending - which is a far better option

How to split long line in Django template?

送分小仙女□ 提交于 2019-12-04 16:56:14
问题 I have too long line in Django template {% for some_item, some_another_item, again_some_another_item_with_long_name in items %} How can I split it? Using \ or just splitting doesn't work. 回答1: If you really want to keep those nasty long names, what I would do is: {% for a, b, c in items %} {% with a as some_item %} {% with b as some_another_item %} {% with c as again_some_another_item_with_long_name %} bla bla bla .. {% endwith %} {% endwith %} {% endwith %} {% endfor %} 回答2: You can use

Add class to form field Django ModelForm

你说的曾经没有我的故事 提交于 2019-12-04 16:41:44
问题 I am trying to write a Bootstrap Form with Django ModelForm. I have read the Django Documentation Django Documentation about Forms, so I have this code: <div class="form-group"> {{ form.subject.errors }} <label for="{{ form.subject.id_for_label }}">Email subject:</label> {{ form.subject }}</div> The {{form.subject}} is rendered by Django, for example in CharField field model, as input tag, <input type="text"....> etc. I need add "form-control" class to every input in order to get Bootstrap

Django: Displaying formset errors correctly

烂漫一生 提交于 2019-12-04 16:41:44
问题 I have an inline formset for a model, which has a unique_together constraint. And so, when I input data, which doesn't fulfill this constraint, it displays: __all__Please correct the duplicate values below. The code, which does this is: {% for error in formset.errors %} {{ error }}<br/> {% endfor %} I don't much like the __all__ at the beginning of the error and it is quite clearly the dictionary key, so I tried: {% for key, error in formset.errors %} {{ key }}: {{ error }}<br/> {% endfor %}

book count per author for filtered book list in django

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 16:41:25
Short question. I have two models: class Author(models.Model): name = models.CharField(max_length=250) class Book(models.Model): title = models.CharField(max_length=250) author = models.ManyToManyField(Author) One view: def filter_books(request): book_list = Book.objects.filter(...) How can I display in template next content: Authors in selected books: Author1: book_count Author2: book_count ... Let's build the query step by step. First, get the authors who have a book in book_list . authors = Author.objects.filter(book__in=book_list) The trick is to realise that an author will appear once for

Configuring django settings to work with 1.4.1. Loading template error

假装没事ソ 提交于 2019-12-04 16:26:54
问题 Here is the error I got: ImproperlyConfigured: Error importing template source loader django.template.loaders.filesystem.load_template_source: "'module' object has no attribute 'load_template_source'" Here is my loader template code: if DEBUG: TEMPLATE_LOADERS = [ 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ] else: TEMPLATE_LOADERS = [ ('django.template.loaders.cached.Loader',( 'django.template.loaders.filesystem.load_template_source',

webapp2, Jinja2: how to cut large html file into multiple html files

本小妞迷上赌 提交于 2019-12-04 15:34:06
When I blog, I like to separate each blog-post into its own .html file (is that ok?) This prevents the file getting too big, and makes it easy to go back and edit a previously written blog post if need be. Occasionally the blog post will contain css/js/ajax/template variables. But on my website, I like all the blog posts on one page (so I can scroll through them all, instead of going to a separate page for each post) Here is an html file that contains two blog posts: {% extends "base.html" %} {% block blog_posts %} <!-- links/targest for the side menu to jump to a post --> <li><a href="#post2"

Issues with feeding data into database when using for loop

喜你入骨 提交于 2019-12-04 15:03:52
In my template I have used for loop for some fields <div class="register_div"> <p>Title:</p> <p>{{ form.title }}</p> </div> <div class="register_div"> <p>Upload Images :</p> <p>{{ form.image }}</p> </div> {% for item in product %} <div class="register_div"> <p>{{ item.Name }} <input type="text" name="custom[{{item.id}}]"/></p> </div> {% endfor %} <div class="register_div"> <p>Price:</p> <p>{{ form.price }}</p> </div> As code shows there is one field which using for loops if that field has three records then in the form it shows three text boxes so that user can feed data to all three fields,