django-templates

Can I suppress newlines after each template tag with Django's template engine?

旧城冷巷雨未停 提交于 2019-12-21 07:34:12
问题 In Rails ERB, you can suppress newlines by adding a trailing hyphen to tags: <ul> <% for @item in @items -%> <li><%= @item %></li> <% end -%> </ul> becomes: <ul> <li>apple</li> <li>banana</li> <li>cacao</li> </ul> Is there a way to do this in Django? (Disclosure: I'm generating a csv file with Django) Edit: Clarified that the newlines I'm hunting down are the ones left behind after the template tags . 回答1: The closest I've found to what you're looking for (I'm looking for the same thing) is

What does request.user refer to in Django?

冷暖自知 提交于 2019-12-21 06:51:03
问题 I have confusion regarding what does request.user refers to in Django? Does it refer to username field in the auth_user table or does it refer to User model instance? I had this doubt because I was not able to access email field in the template using {{request.user.username}} or {{user.username}} . So instead I did following in views file: userr = User.objects.get(username=request.user) And passed userr to the template and accessed email field as {{ userr.email }} . Although its working but I

How to write better template tags in django

陌路散爱 提交于 2019-12-21 06:07:28
问题 I've seen how I can write template tags that set a context variable based on a template like this {% my_template_tag 'blah' as my_context_variable %} But I'd like to be able to do this: given that both group and user are set in the context in the view {% is_in_group group user as is_member %} {% if is_member %} #.... do stuff .... {% endif %} or ideally something like this: {% if is_in_group group user %} # .... {% end if %} Obviously the other way round is to just set is_member in the view -

django form: how to check out checkbox state in template

为君一笑 提交于 2019-12-21 05:33:14
问题 I have a form with checkboxes that works fine, but when the user submits the form with errors and my checkbox is checked I need to change div class which holds the checkbox. So I was experimenting with {{ form.mycheckbox.data }} in the template and it says False on page load. Now when user is click on a checkbox and the form has errors, it says on . So I tried: {{ if form.mycheckbox.data == True }} doesn't work {{ if form.mycheckbox.data != False }} doesn't work {{ if form.mycheckbox.data ==

Django, templates, for loops, and cycles

妖精的绣舞 提交于 2019-12-21 04:59:21
问题 (tl;dr at the bottom) Let me try to explain what I'm trying to accomplish: I have a two dimensional array, and I would like to display its contents a certain way. I want "rows", and each row can display no more than three "objects", for lack of a better word. So I want to iterate over the array and create my HTML in the process. My idea is this: every "first of three" elements in the array should open up the "row". Every "third of three" elements should close the "row". However, if the last

Django NoReverseMatch

时光总嘲笑我的痴心妄想 提交于 2019-12-21 04:52:50
问题 I have the following setup: /landing_pages views.py urls.py In urls.py I have the following which works when I try to access /competition : from django.conf.urls.defaults import * from django.conf import settings from django.views.generic.simple import direct_to_template from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"), ) My views.py has something like

Django - How to add html attributes to forms on templates

♀尐吖头ヾ 提交于 2019-12-21 04:51:30
问题 Suppose I have the following template: <div id="openid_input_area"> {{ form.openid_identifier }} <input name="bsignin" type="submit" value="{% trans "Sign in" %}"> </div> How can I add a css class to form.openid_identifier? As I am adhering to the SOC principles and I expect designers to improve the templates I do NOT want to do this on the form model but on the template itself. I couldn't find anything on this on the docs. Is there a way to do this on the template? 回答1: I've managed to code

Django - How to add html attributes to forms on templates

拟墨画扇 提交于 2019-12-21 04:51:18
问题 Suppose I have the following template: <div id="openid_input_area"> {{ form.openid_identifier }} <input name="bsignin" type="submit" value="{% trans "Sign in" %}"> </div> How can I add a css class to form.openid_identifier? As I am adhering to the SOC principles and I expect designers to improve the templates I do NOT want to do this on the form model but on the template itself. I couldn't find anything on this on the docs. Is there a way to do this on the template? 回答1: I've managed to code

how to render django template variable as html?

会有一股神秘感。 提交于 2019-12-21 03:51:05
问题 What I want is like stack overflow. User can HTML format their text input, and the page should be rendered exactly in the same way, I use the wmd.js to store the formatted input, Consider I have a context variable {{variable}} with string value "<p>something</p>" . When I render the template, {{variable}} outputs <p>something</p> and {{variable|safe}} also output <p>something</p> It shows the html tag as text in the page. How to render the HTML tag in the {{variable}} but not showing them as

Overwriting a block within an `include`d template from an extended template

£可爱£侵袭症+ 提交于 2019-12-21 03:41:10
问题 I have the following: base.html <html> {% include 'header.html' %} <div> {% block content %}Default Content{% endblock %} </div> </html> header.html <header> {% block logo %}Logo 1{% endblock %} </header> homepage.html {% extend 'base.html' %} {% block logo %}Logo 2{% endblock %} {% block content %}Yap Yap Yap{% endblock %} Essentially, this doesn't work. When I render the homepage.html I get: <html> <header>Logo 1</header> <div>Yap Yap Yap</div> </html> but If I move the code in header.html