django-templates

Compare request.path with a reversed url in Django template

五迷三道 提交于 2019-12-07 04:50:18
问题 I understand that request.path will give me the current URL. I am currently working on my base.html template with CSS tabs and I want the template to know which tab is currently "active" and pass class="active-tab" to an <a> tag. So I wanted to do something like <a href="{% url orders_list %}" {% if request.path = reverse('orders_list') %} class="active-tab" {$ endif %} >Orders</a> But I'm sure you can't do that if comparison. I also only want the base (?) URL ignoring any GET parameters. Any

Get only one field from django forms in template

别来无恙 提交于 2019-12-07 04:06:59
问题 I have one form : class FormLogin(forms.Form): email = forms.EmailField(max_length=150) name = forms.CharField(max_length=20) How can I put just email field in my template ? I tried this : {{ form.fields.email }} But it returns <django.forms.fields.CharField object at 0x00000000043BCEB8> . 回答1: You can just use: {{ form.email }} 回答2: You don't need to use fields . Use: {{ form.email }} 来源: https://stackoverflow.com/questions/13521634/get-only-one-field-from-django-forms-in-template

how to split the string in django template?

▼魔方 西西 提交于 2019-12-07 03:38:39
问题 i am trying to split the string in template using custom template filter. But i got an error TemplateSyntaxError at /job/16/ 'for' statements should use the format 'for x in y': for skill in form.instance.skills | split : "," Here it is my filter @register.filter(name='split') def split(value, key): """ Returns the value turned into a list. """ return value.split(key) this is my template <h4>Skills</h4> {% for skill in form.instance.skills | split : "," %} {{ skill }} {% endfor %} Thanks 回答1:

Facebook like and share button on ajax

被刻印的时光 ゝ 提交于 2019-12-07 03:37:07
问题 Background: - Am using ajax to get a entity called as "foo_posts". In this post am using facebook share and like button {% for post in foo_posts %} <div class="foo"> {{ post }} <div class="fb-like" data-send="true" data-width="450" data-href="http://foo/foo/detail/{{ foo.id }}/" data-show-faces="true"></div> </div> {% endfor %} Now these posts are being populated using Ajax. Problem: - The facebook like and share gets initialized by $(function(d, s, id) { var js, fjs = d.getElementsByTagName

How to concisely represent if/else to specify CSS classes in Django templates

懵懂的女人 提交于 2019-12-07 03:11:11
问题 In a Django template, I'd like to add CSS classes to a DIV based on certain "conditions", for example: <div class="pkg-buildinfo {% if v.release.version == pkg.b.release.version %}active{% else %}inactive{% endif %} {% if v.release.version == project.latest.version %}latest{% else %}notlatest{% endif %}"> ( note that v is a loop variable; the whole thing is inside a for loop) The above adds CSS classes "active" or "inactive" and "latest" or "notlatest" based on two conditions. This is however

Custom base_site.html not working in Django

五迷三道 提交于 2019-12-07 03:03:51
问题 I am using Nitrous for playing with the Django framework. In tutorial 2 is shown how to change the base_site.html template. I've added in the TEMPLATE_DIRS = ( ) a new line: 'home/action/workspace/mysite/templates', And in base_site.html I changed the site name title of Django Administration into Administration: {% trans 'Administration' %} But I still see no changes on the website. I've tried different TEMPLATE_DIRS like: '~/workspace/mysite/templates', 'home/action/workspace/mysite/', 'home

Is django can modify variable value in template?

限于喜欢 提交于 2019-12-07 02:59:21
问题 I want to write template that render somethings only one time. My ideas is create flag variable for check it is first time or not ? My code {% with "true" as data %} {% if data == "true" %} //do somethings ** set data to "false" ** {% else %} //do somethings {% endif %} {% endwith %} I don't know How to change variable in django template ? (Is it possible ?) Or Better way for do this. Thank you 回答1: This can be done with a Django custom filter django custom filter def update_variable(value):

Django - Custom Filter to check if File exists

时光毁灭记忆、已成空白 提交于 2019-12-07 02:15:03
问题 I made this custom filter to check if an image exists or not: from django import template from django.core.files.storage import default_storage register = template.Library() @register.filter(name='file_exists') def file_exists(filepath): if default_storage.exists(filepath): return filepath else: index = filepath.rfind('/') new_filepath = filepath[:index] + '/image.png' return new_filepath And I used this in the template like this: <img src="{{ STATIC_URL }}images/{{ book.imageurl }}|file

Access form field attributes in templated Django

孤街浪徒 提交于 2019-12-07 01:27:39
问题 I've been doing some custom forms with django but I don't get how to access attributes that a specific form field has attached via the forms.py. def putErrorInTitle (cls): init = cls.__init__ def __init__ (self, *args, **kwargs): init(self, *args, **kwargs) if self.errors: for field_error in self.errors: self.fields[field_error].widget.attrs['title'] = self.errors[field_error][0] self.fields[field_error].widget.attrs['class'] = "help_text error_field" cls.__init__ = __init__ return cls That's

How to truncate html without breaking the tags?

孤人 提交于 2019-12-07 00:04:35
问题 How to ensure that all html-tags were closed? The problem arises because I want create some sort of excerpt for every article. For example someone writes an article like this: Hi everyone, I'm just an article and I have few <strong>tags</strong> inside <em>of me</me> If I cut this message just after "tags", I get an unclosed tag . How can I check with Django all user's input text before saving it to DB? 回答1: In Django 1.7, there is a specific template filter called truncatechars_html: Similar