django-templates

Building HTML with templates versus building it in javascript?

限于喜欢 提交于 2019-12-22 06:17:38
问题 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

Django annotated value in template

六月ゝ 毕业季﹏ 提交于 2019-12-22 05:33:14
问题 Is it possible to access annotated values on querysets in templates? For example I have the following queryset that I'm passing to my template: context[videos] = Videos.objects.annotate(view_count=Count(views)).order_by(view_count)[:100] In my template I'm trying to get the view count like this: {% for video in videos %} {{ video.view_count }} {% endfor %} Which displays nothing. However if I use: {{ video.views.count }} It seems fine - but i believe the second option recalculates the view

Django 1.8 with Jinja2: Contrib app Admin not working

自闭症网瘾萝莉.ら 提交于 2019-12-22 05:25:31
问题 I upgraded to a fresh install of Django 1.8 and began using Jinja2 as it said that it was supported now and Jinja2 has some features I could use in my project. After finishing adapting the templates for my app to Jinja2 and taking advantage of the new features now available, I discovered that the contrib app Admin no longer works. "TemplateDoesNotExist at /admin/login/" So it turns out that contrib app Admin only has templates made for DjangoTemplates and not for Jinja2. I did the naive thing

Is it safe to render user-created Django templates?

☆樱花仙子☆ 提交于 2019-12-22 04:45:24
问题 Is it safe to let users make their own Django templates with a set of pre-defined variables, and then render this template on the server? I would only pass a very limited set of parameters to render , all of which are strings. Templates would be something like: hey, my name is {{name}}. So, the question is, are there any django template tags that can be abused to get information that users are not supposed to get? I'm most worried about the {% url %} tag. P.S. I noticed this question after

save multiple uploaded files in django

我与影子孤独终老i 提交于 2019-12-22 04:36:25
问题 I want to upload and save multiple files in my application, I have <input type="text" name="name" value="" /> <input type="file" name="file" multiple/> in my template. when I hit upload, seems form = MyForm(request.POST, request.FILES) is only saving one file which is last in the list of the many uloaded files. How can I be able to save all the uploaded files using the form form = MyForm(request.POST, request.FILES) blah blah ? Thanks Edit Myform is a model form from this model. class Docs

Django 'ascii' codec can't encode character

我的未来我决定 提交于 2019-12-22 02:59:50
问题 In Django I want to use a simple template tag to truncate data. This is what I have so far: @register.filter(name='truncate_simple') def truncate_char_to_space(value, arg): """ Truncates a string after a given length. """ data = str(value) if len(value) < arg: return data if data.find(' ', arg, arg+5) == -1: return data[:arg] + '...' else: return data[:arg] + data[arg:data.find(' ', arg)] + '...' But when I use it I get the following error: {{ item.content|truncate_simple:5 }} Error: 'ascii'

Django-grappelli admin: No reverse match error

Deadly 提交于 2019-12-22 02:56:17
问题 I've been working on a django project for a while now that uses grappelli for the admin and all of a sudden today my change_form.html template is throwing the following error: Caught NoReverseMatch while rendering: Reverse for "grp_related_lookup" with arguments '()' and keyword arguments '{}' not found. The offending line of code is line 38: 37 $.each(related_lookup_fields_fk, function() { 38 $("#id_" + this).grp_related_fk({lookup_url:"{% url grp_related_lookup %}"}); 39 }); which is

initial value for django form choice field ignored

我与影子孤独终老i 提交于 2019-12-22 02:30:42
问题 I have this form: class UserUsesSourceForm(forms.Form): # some fields here username = forms.CharField(label=("Username"), max_length=30, help_text = ("Required")) provider = forms.ChoiceField(widget=forms.Select(), choices=SOURCES_CHOICES, initial=SOURCES_CHOICES[1]) The available choices are: E = 'e' A = 'a' SOURCES_CHOICES = ( (A, 'A'), (E, 'E'), ) The view: form = UserUsesSourceForm(initial={"username":request.user.username, 'provider':SOURCES_CHOICES[1]})return render_to_response('update

Client-Side (JavaScript) Django/Jinja-like template inheritance

馋奶兔 提交于 2019-12-22 02:25:11
问题 I'm looking for a way to do template inheritance to a group of .html files I have. Let's say I have a base.html file which has the common HTML for all pages of my website, i.e. header, footer, etc. . Each page, including the main (index) page, needs to inherit from this template HTML file. Now, this is doable on the server-side using Django's Jinja template langauge. But this is not a good solution for me. My HTML pages are stored remotely and I have no control over the server storing them.

How do I get a “debug” variable in my Django template context?

∥☆過路亽.° 提交于 2019-12-22 02:01:48
问题 According to this SO post: How to check the TEMPLATE_DEBUG flag in a django template? if: A) my settings.py file has: TEMPLATE_CONTEXT_PROCESSORS = ['django.core.context_processors.debug',... and B) I use a RequestContext (as opposed to a Context) I should have a "debug" variable to my template context. However, I don't: when I do {{debug}} in a template, it renders as nothing (""). Is there anything else I'm missing that is necessary to get a "debug" var in the template context? 回答1: You