FAQ Database Discussion Community


Template Tag in Javascript

javascript,django,django-templates,django-template-filters
My Django template tags are not working within my javascript. My latest error is: SyntaxError: expected expression, got '&' var resourceTypes = ['Structural Model', 'X-Ray Diffraction'] How can I get this to work? I need to get these Django variables into the js so that I can create a chart...

Is it possible to display a specific block from a template in django?

django,django-templates,django-views,django-urls,django-template-filters
Just like in html when you reference a section of a page with for instance<a href="#tips">Visit the Useful Tips Section</a> Would there be a way to do a similar thing in django if for instance I wanted to load my page straight to the tips section? I am extending base.html...

'and' condition in for loop in django template

python,django,django-template-filters
Hi I want to check 2 or more conditions in a for loop inside django template. I tried - <table> {% for p, q in products, quantity %} <tr> <td>{{ p.p_name }}</td> <td>{{ q }}</td> </tr> {% endfor %} </table> But it did not helped and gave error - 'for'...

F() Expression strange behavior with Django

python,django,django-models,django-templates,django-template-filters
I have this template tag that ultimately returns a list of 'active' advertisements (checks if Campaign with the active field is True, then pulls ads from the Campaign with a queryset) @register.assignment_tag def get_current_campaigns(amount): # Get all the campaigns that are active current_campaigns = Campaign.objects.filter(active=True) current_campaigns_count = current_campaigns.count() # To...

Django template: how to randomize order when populating page with objects?

python,html,django,python-3.x,django-template-filters
I have a Survey and a Choice model, and each survey has a number of choices associated with it. When I'm rendering the actual HTML survey page with all the choices, I use the following Django templating code: {% for choice in survey.choice_set.all %} <li class="ui-state-default" choice_id={{ choice.id }}>{{ choice.choice_text...

Accessing list item in django template

python,django,django-templates,django-template-filters
I have a list, rate_text ['', 'Disappointed', 'Not Promissing', 'OK', 'Good', 'Awesome'] in the template rendered from the view. I can access any of the item of that using {{rate_text.index}} like the following: {% for review in reviews %} <div class="panel panel-info" style='border-color:#ffffff;'> <div class="panel-heading" > <h3 class="panel-title lead">{{review.title}}</h3> </div>...

What does Django Date filter mean by 'Naive'?

python,django,date,django-template-filters
I want to use the 'c' date format with the Django date filter. That format references 'naive' dates. I don't want to have timezones in my template (that's held elsewhere in my xml). I'm not sure what that is, the documentation for django doesn't mention it, nor does the PHP...

Show message when there's no excerpt - Django templates

python,django,django-templates,django-template-filters
I have this field on a Django template: <p class="border_dotted_bottom"> {{ expert.description|slice:":300" }} <a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>.... </p> If this object (user) has no 'decription' (a text field) it shows the word 'None', I need to get rid of that, maybe if he doesn't...

slice the middle of a dynamically generated string in Django

django,django-template-filters
Is there a way (Using Django filters or any other language) to find and slice certain parts of a dynamically generated string? I have tried the slice method and the truncate method {{variable|slice:"130:-60"}} or {{variable|truncatechars:255 }} but neither of those methods work exactly right..... I am working on weather alerts...

Django template filter keeps giving “unsupported operand type”

python,django,datetime,django-templates,django-template-filters
I keep getting this error with my template filter: "unsupported operand type(s) for -: 'datetime.datetime' and 'datetime.date'" I've looked at many other answers dealing with this error but I can't seem to find what's wrong. Any pointers much appreciated. This is my template filter: @register.filter(name='get_due_date_string') def get_due_date_string(value): delta = value...