django-templates

Calling a method on a model from a template

I am trying to call a method in a model from a template and I have come to the conclusion that this cannot be done. This is my code {% if request.user.is_authenticated %} {% if a_story.is_story_liked(request.user.id) %} <a class="story_like" data-id="{{ a_story.id }}" href="#">Like</a> {% endif %} {% else %} <a c...

django: generic views + custom template tags or custom views + generic/normal template tags

This is more of a best-practices question, and given that I'm quite tired it mightn't make much sense. I've been putting together a blog app as a learning experience and as an actual part of a website I am developing. I've designed it like most apps so that you can list blog posts by multiple criteria i.e. /blog/categories/ /blog/au...

Accessing session variable in Django template with Google App Engine (Webapp) - Python

Hi, I have a Django template as my front-end. At the back-end, I used the sessions provided from Gaeutilities to store a variable (email). Front-end: {% if session.Email %} <div id="entersite">WELCOME <em>{{session.Email}}</em></div> {% else %} <div id= "entersite"><a href="/login/" id= "entersite">Enter the Site</...

Access Model Data from Django Base Template

I have a model Category, and I want its objects to always be displayed in a navigation menu in my base.html template (which all of my other templates extend). I want to learn best-practices so would like to know what the correct/accepted way of providing this data to the template is. ...

Caught NoReverseMatch while rendering with generic PasswordChangeForm Django view

I'm trying to use the generic PasswordChangeForm and password_change view in Django, and running into a NoReverseMatch error. Here is my urls.py: (r'^change_pass/','django.contrib.auth.views.password_change', {'post_change_redirect':'/home'}), Then, I'm trying to call the following from the template: <form action="{% url django....

views created in django-page-CMS disables all CSS? code included, a fix please

I am using Django-page-CMS Everything works fine However once I create my own views which extend from pages used within the CMS the CSS does not display. This is strange because these pages display the CSS fine, as long as I do not use my own views. I would greatly appreciate some help on this matter or at least some suggestions on wh...

How do you limit list objects template side, rather than view side

One of the ways to limit objects is to add a limitation to a function like this def ---(): ---- = ---.objects.all()[0:10] #limit to 10 return {'objects': objects} However how do you achieve this inside a template rather than inside a view? I know you can filter through objects within a template and limit characters but how do you ac...

How do you display only specific variable information within a django-template ?

For example If I have the following models, views and code in a template... class news(models.Model): type = models.ForeignKey(----) (charfield) title = models.CharField(max_length=100) published = models.DateTimeField(default=datetime.now) summary = models.CharField(max_length=200) def ----(): items = news.objects...

Django: How to check if field widget is checkbox in the template?

I've created a custom template for rendering form fields: <tr class="{{field.field.widget.attrs.class}}"> <th class="label"> <label for="{{field.auto_id}}"> {{field.label}} {% if not field.field.required %}<span class="optional">(optional)</span>{% endif %} </label> </th> <td class="fi...

Django admin site: How does the Add User page works (more fields on edit) ?

I was wondering how they made it possible to display more fields in the User page of the djagno admin site. If you create a new User you only have some basic fields to fill in but if you reopen that user (edit mode) then you see a lot more fields to fill in. Im trying to achieve the same, I had a look at the add_form.html template but I...

Django: most efficient way to query many records?

Hello, I have a table with a few thousands records (products). Each product has a 4 different categories: CAT1 CAT2 CAT3 CAT4 I wonder if is there a method, or what is the best practice, to dynamically retrive the available categories based on the categories already selected (using Ajax). Example: if CAT1 = green all the products wit...

Dynamically choosing template for django inclusion tag

Currently I have an inclusion tag that is coded something like this: @register.inclusion_tag('forms/my_insert.html', takes_context=True) def my_insert(context): # set up some other variables for the context return context In my template, I include it by putting in {% my_insert %} New Feature Request We now want to test a ne...

Expense of django query

I have two tables: Actor and Images. The Images table is linked to the Actor table through a Foreign Key. I might make a query on Actor that pulls a list of all the actors with first name beginning with the letter A. Let's say I store this query result in actor_list and pass it to the template. I can get a list of all the Images thro...

Idiosyncracy while extending django admin template

On my django site, I decided to just use the admin templates for the UI, but I made a few tweaks like the site name, color, etc. even my custom views just extend admin/base_site.html I did this by creating templates/admin/base_site.html with the following code: {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }...

Dynamic blocks in django templates

Hi, It is a question about django that has found absolutely no answer for me. Let's suppose I have a site where I display two blocks in the sidebar : A list of the last users who've logged in A list of the last published blog articles Let's say that these blocks are to be displayed on 80% of the website urls and presented using templ...

Django: Overriding AND extending an app template

If you want to override a template coming with an app in django (in app/templates/app/) you create a template of the same name in another directory, which the template loader checks before the app's template dir. If you just want to override certain blocks of the template you also have to copy the whole template ad change that block, whi...

How to use Django ORM to get a list by year of all articles with an article count

Hello, I am trying use the django ORM to get a list by year of all my articles with an article count beside it, such as this: 2010 (5 articles) 2009 (4 articles) 2008 (9 articles) I have tried things such as: archive = Articles.objects.dates('created', 'year').annotate(archive_count=Count('created')) or: archive = Articles.objects.v...

Showing the Foreign Key value in Django template

Hello everyone. Here is my issue. I am new to python/django (about 2 months in). I have 2 tables, Project and Status. I have a foreign key pointing from status to project, and I am looking to try to display the value of the foreign key (status) on my project template, instead of the address of the foreign key. Here is my models.py...

sending email templates in with django

Hello, I want to send an email with a template like this. {% extends "base.html" %} {% block content %} <h2>Invoice Details</h2> <div id="horizontalnav"> <a href="/index/add_invoice">Add an Invoice</a> <a href="/index/work_orders">Add a Work Order</a> <a href="/index/add_payment">Add Payment</a> </div> <ul STYLE="border: 1px soli...

How do you display image objects in a Django template using a custom template tag? (coding included)

I have the following code that fails to display object images. But displays normal images fine. My Model class News(models.Model): title----------- image = models.ImageField(upload_to='images') body------------ Template tag coding from django import template register = template.Library() from ----.models import --- def funct(num): ...