django-templates

Django - verbose_name from a template tag

纵然是瞬间 提交于 2019-12-10 11:18:39
问题 I need to display several models name & objects in a template Here is my view def contents(request): """Lists Objects""" objects = [ Model1.objects.all(), Model2.objects.all(), Model3.objects.all(), Model4.objects.all(), ... ] return render_to_response('content/contents.html', objs , context_instance=RequestContext(request) ) My template {% for objs in objects %} <div class="object"> <div class="object_name">{{ get_verbose_name objs.0 }}</div> <ul> {% for obj in objs %} <li>{{ obj }}</li> {%

Django: How to return model formset in ajax and use in template

无人久伴 提交于 2019-12-10 10:48:23
问题 I need to dynamically add forms to my formset during runtime using ajax, for which I am referring to Dynamically adding a form to a Django formset with Ajax I have multiple formsets on the same page with different prefixes. My models are designed like so: A user can have many phones. A phone can have many lines (if details are needed) Accessing Many to Many "through" relation fields in Formsets Once a user adds a new phone, I save the phone using ajax. The view is as follows def addUserPhone

Localization with django and xgettext

回眸只為那壹抹淺笑 提交于 2019-12-10 10:16:38
问题 I am working on translation of my site, but when I try to run command: manage.py makemessages --locale=bs I constantly get error like: CommandError: errors happened while running xgettext on rjsmin.py xgettext: Non-ASCII string at .\compressor\filters\jsmin\rjsmin.py:59. Please specify the source encoding through --from-code. What shell I do? 回答1: I have the same problem. At the top of the rjsmin.py, change the "- * - coding: ascii -*-" to "- * - coding: utf-8 - * -" 来源: https://stackoverflow

how to use custom django templatetag with django template if statement?

时光怂恿深爱的人放手 提交于 2019-12-10 10:15:40
问题 I've made a django template tag that counts one of my custom user many-to-many field length: from django import template register = template.Library() @register.simple_tag(takes_context=True) def unread_messages_count(context): user = context['request'].user return len(user.messages_unread.all()) and within the template itself, I want to show it to user only if it's larger than zero, so I tried: {% ifnotequal unread_messages_count 0 %} some code... {% endifnotequal %} but obviously it didn't

Load Django Form Object with data from Model instance already “loaded”

那年仲夏 提交于 2019-12-10 10:12:36
问题 I have the following type of template in Django: <form .... > <label class="control-label" for="id_quantity">Quantiy</label> {{ form.quantity }} <label class="control-label" for="id_title">Quantiy</label> {{ form.title}} </form> The view contains a ModelForm object in its context from which the "form" variable in the template is derived. Often, you can pass a form that is bound to data like request.POST. How do I load a form that is bound to the data of a specific instance of a model? Is

Django: Can't override admin templates when they are already overridden?

随声附和 提交于 2019-12-10 09:56:12
问题 To add some content to a Django admin view of a model, I want to override the change_form.html template. According to the documentation I would need to create a file change_form.html in a the folder /project-path/templates/admin/appname/modelname/ . Of course I need to make sure, that this path is also available in TEMPLATE_DIRS . Such a file could look like this: {% extends "admin/change_form.html" %} {% load i18n %} {% block after_field_sets %} SOME CONTENT {% endblock %} However, I make

Django template variables from {% for %} loop into Javascript

こ雲淡風輕ζ 提交于 2019-12-10 09:42:30
问题 This is a Django template iterating through records. Each record contains a div that JS function fills in. In order for JS to know what to do, it needs to get a variable from each for loop iteration and use it. I don't know exactly how to achieve that or if it's possible. Maybe a different approach is needed where ever record triggers a timer in a separate JS object instance, I don't know. ---------- html ----------------------- {% for match in upcoming_matches %} ... <tr> <td> Match title <

Python Django custom template tags register.assignment_tag not working

早过忘川 提交于 2019-12-10 04:10:04
问题 This is my Code for Python Django custom template tags from django import template from ipc.declarations.models import MainDeclaration from django.shortcuts import get_object_or_404 register = template.Library() def section_settings(declarationId,user): declaration = get_object_or_404(MainDeclaration, pk=declarationId, user=user) businessInfo = declaration.GetOrCreateBusinessInfo() sections = declaration.GetSections() return sections register.assignment_tag(section_settings) Now i am getting

Django - CreateView - How to declare variable and use it in templates

℡╲_俬逩灬. 提交于 2019-12-10 03:57:51
问题 How do I declare a variable in Django's Createview, so I can use it from its template? For example I want to use {{ place_slug }} in the template. I pass that from urls.py like below: urls.py: urlpatterns = patterns('', (r'^new/(?P<place_slug>[\w\-\_]+)/?$', PictureCreateView.as_view(), {}, 'upload-new'), ) views.py: class PictureCreateView(CreateView): model = Picture def dispatch(self, *args, **kwargs): self.place = get_object_or_404(Place, slug=kwargs['place_slug']) return super

TemplateSyntaxError 'staticfiles' is not a valid tag library'

[亡魂溺海] 提交于 2019-12-10 02:39:02
问题 I'm having a really strange issue trying to get the staticfiles taglib working in my application. I'm essentially getting the following error: 'staticfiles' is not a valid tag library: Template library staticfiles not found, tried django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles Here's my template which is throwing this error: {% load staticfiles %} <html> <head> {% block stylesheets %} <link rel="stylesheet" href="{% static "styles/bootstrap-1.2.0.min.css" %}"> {