django-templates

Using Django FormPreview the right way

ぐ巨炮叔叔 提交于 2019-12-22 10:39:56
问题 My Goal I have a django project with a form, and I want to display a preview page before the user submits. The problem I can display a preview page using a Django FormPreview, but not all form data is displayed properly. Specifically, if I have a field with choices , the string values of these choices aren't displayed. I'm also having problems applying template filters to date fields. The end result is that some data on the preview page is visible but other data is blank: However, if I

Django template with jquery: Ajax update on existing page

别说谁变了你拦得住时间么 提交于 2019-12-22 10:33:44
问题 I have a Google App Engine that has a form. When the user clicks on the submit button, AJAX operation will be called, and the server will output something to append to the end of the very page where it comes from. How, I have a Django template, and I intend to use jquery. I have the following view: <html> <head> <title></title> <script type="text/javascript" src="scripts/jquery.js"></script> <script type="text/javascript" src="scripts/scripts.js"></script> </head> <body> welcome <form id=

Use field label as placeholder with django-widget-tweaks

亡梦爱人 提交于 2019-12-22 10:30:12
问题 I am using django-widget-tweaks and unable to figure out how to add a field variable as placeholder , like the following: <div class="col-sm-10"> {{ field|append_attr:"class:form-control"|append_attr:"placeholder:field.label" }} {% if field.help_text %} <p class="help-block"><small>{{ field.help_text }}</small></p> {% endif %} </div> field.label above does not evaluate and puts the string "field.label" as the placeholder on the page. Some SO posts suggest registering a custom tag/filter which

Interpolate Django template include variable

放肆的年华 提交于 2019-12-22 10:05:54
问题 I'm doing something like {% for part in parts %} {% include "inc.html" with o=part prefix="part{{ forloop.counter0 }}_" %} {% endfor %} where inc.html could be something of such kind: <p id="{{ prefix }}para">{{ o.text }}</p> I just discovered the prefix variable isn't interpolated and "part{{ forloop.counter0 }}_" is passed literally. Any relatively elegant work-around? 回答1: I think the best solution would be to register an inclusion_tag, that would handle the part and forloop.counter

CSS Templating system for Django / Python?

主宰稳场 提交于 2019-12-22 08:46:24
问题 I'm wondering if there is anything like Django's HTML templating system, for for CSS.. my searches on this aren't turning up anything of use. I am aware of things like SASS and CleverCSS but, as far as I can tell, these still don't solve my issue as I want to dynamically generate a CSS file based on certain conditions, so that a different CSS file would be served based on a specific user session... I want to minimize the use of javascript / AJAX for some things (since its for a legacy system

Django : ModelForm with conditions

╄→гoц情女王★ 提交于 2019-12-22 08:36:23
问题 I'm trying to create a form variable. As default Player have the level 0 and he can just change is name. Later when he is level 1, he can change is name and is avatar. When he is level 3, he can change is name, is avatar and is job. Etc... Models.py: class Player(models.Model): level = models.SmallIntegerField(default=0) name = models.CharField(max_length=50) avatar = models.URLField(default='http://default-picture.com/01.png') job = models.TextField(null=True) Fomrs.py: class ProfileForm

Check if url matches in template

左心房为你撑大大i 提交于 2019-12-22 07:06:11
问题 Is it possible to check in template that some url match any pattern from urls? 回答1: This is something you'd normally want to do in a views.py file with the reverse() helper for named URLs with known args or resolve() for paths. If you do need this functionality in a template specifically, here is a hacky solution: @register.simple_tag def urlpath_exists(name): """Returns True for successful resolves()'s.""" try: return bool(resolve(path)) except Resolver404: return False Note : this doesn't

Django template cycle for alternating rows - without loop

狂风中的少年 提交于 2019-12-22 07:05:04
问题 Perhaps this is a non-question, but how do you make use of the Django {% cycle %} functionality, or something similar, when you're not in a loop? Specifically, I have an HTML table that I'm writing by hand, since it's not the sort of thing you need to do in a loop. I want the rows to alternate, like this: <tr class="{% cycle 'even' 'odd'%}"></tr> <tr class="{% cycle 'even' 'odd'%}"></tr> <tr class="{% cycle 'even' 'odd'%}"></tr> But I'm not using a loop, so this always results in even . I don

Is it possible to apply a template tag to a <td> when using django-tables2?

落花浮王杯 提交于 2019-12-22 07:01:39
问题 I am using django-tables2 to create my table for me. I need to apply a template tag to each of the cells () in one of the columns. It seems like alot of extra effort to go through and create a custom table layout just to apply the template tag to the one column. Is there a way to do this in django-tables2? Update: I may not have explained what I'm looking for well enough. I don't believe that will work. My code: class CombineTable(tables.Table): build_no = tables.LinkColumn('run', args=[A(

Building HTML with templates versus building it in javascript?

淺唱寂寞╮ 提交于 2019-12-22 06:18:07
问题 I recently started making web apps, and while I make some stuff that works, I'm not sure of the best practices. Up to now, I've mostly used the templating systems of Django, web.py and PHP. But now that I'm using jQuery and nice ajaxy magic to get data from the server without refreshing the client, I'm seeing the advantages of building the HTML directly in javascript (so I can just send a small json object to the client and have him figure out what to change and how). So now I have some bits