Access Model.objects methods from Django templates - python

Let's say I have a "Person" model.
How can I display the number of persons in my system in a template?
In standard code, I would do: Person.objects.count().
But how to do this in a template?

You save the output of Person.objects.count() in a variable and pass it on to your template from the corresponding view.

Related

querying/filtering a list of data inside django template

how do i do a filter in a django template? lets say that i have a model like this
class Entity:
attribute1
attribute2
something like that now i have a form.py to submit data into my database
Class EntityForm:
Class Meta:
model=Entity
now in the template i have my form
and in my view i have the contorller of my form
so when everything works fine i get all the data i wanted and now i have so many data that i submited that i want to search for just specific results, now i want to filter/query all the data throught my template, is this posible? how?
i was wondering if i should do something hard because im still learning, i wanted to do a
{% entity.objects.filter(atribute2='something') %}
but this doesnt work! any idea?
thanks!
Why do you want to do that in your template? If the user submitted a search query, that goes to your view first, so that is the appropriate place to do the filter.
As is clearly documented, the template language is expressly restricted, so that you can't call functions with parameters.

Howto use Django class based UpdateViews with FileFields

I have a simple model containing a FileField among others. When I use a class based UpdateView It is possible to modify all fields of the model and changes are saved to the database on submit.The only exception to this is the FileField, while it is displayed as part of the form, and I can choose I file I want to upload nothing is saved.
Question:
How can I use FileFields in class-based UpdateViews, is there specific code required to handle those fields?
Make sure your form is declared like so:
<form enctype="multipart/form-data" method="post" action="/foo/">

Django: How to import variables in html templates

I have a django application and what I want to do is change wherever it says "Company id" in my templates. The thing it can be very tedious because I have to make this change in every template which says "Company id". So then I thought I might create another file that can store this entry, which the I can easy custom the company id.
config.py
company_no = "Company id"
This can work in my forms.py file. I can import company_no by saying
forms.py
from mmc.config import company_no
But then how can I do the same thing for templates? Importing company_no in a template - is there a way round?
This is what context processors are for. Define your company name in settings.py, then write a context processor that imports it from there and returns it in a dictionary - it will then be available in every template (as long as you use RequestContext to render the template).
As Blender stated, you need to pass variables like this in as part of the context when you render the template. You might make a dictionary or a namedtuple that has common items stored in configuration loaded in a function.
You should also consider using template inheritance if many templates will be display the same data, then you can have methods that load the pieces of context that go with certain base templates.
You could create a shared template and use include to load it in to the main template. Then in the shared template you could load and call a custom template tag that produces a context variable and render it as usual.
Alternatively, you could create a custom context processor that loads the data automatically in to the context instance and then render it as usual in the shared template.

Add Derived Field to Django Admin Change Form

I'd like to add a derived field to a default ModelAdmin.fieldsets like I would by specifying a method and adding it to the ModelAdmin.list_display property, but there doesn't seem to be an easy way to do that (if there is ANY way to do that).
The default Django Admin list view seems to have a lot more options than the change form view does.
Let's say I have two fields for a location: latitude and longitude, and instead of displaying them on a change form I want to display a Google Maps Static Map image instead - I already have a method that will return the src url for the image - I just need a way to add that image to the model change form instead of showing those two fields.
If you write a method and add it to ModelAdmin.readonly_fields, it will appear on the change view.
customize admin view http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ref-contrib-admin

Django Formset without instance

In this Django Doc explain how to create a formset that allows you to edit books belonging to a particular author.
What I want to do is: Create a formset that allows you to ADD new book belonging to a NEW author... Add the Book and their Authors in the same formset.
Can you gime a light? thanks.
When you're instantiating the form and formset for the initial display, you don't need to provide an instance - so you will just get blank forms.
When you pass in the data on POST, you can do the form first, save it, and get an instance. Then you can pass that instance into the formset, so that it correctly saves the related objects.
This depends on whether you're doing it yourself, or using the built-in admin.
If you're using the admin, you can use inlines.
If you're doing this in your own application, then it's up to you. Create a single form which has fields for a new author and book. When the user submits the form, it's your job to create the new records.

Categories