abidibo.net

Posted in "django"

Show google analytics statistics in django admin

django django-admin

In this post we'll see how to show google analytics statistics inside your admin index. If you use django-suit as your admin application then you could simply install django-otto-admin and follow the provided instructions to get it working.

Requirements

First of all we need to create a google developer project and a service account which will allow us to authenticate to the google analytics account.

Follow the steps in the Google Identity Platform documentation to create a service account from the Google Developer Console.

Once the service account is created, you can click the Generate New JSON Key button to create and download the key and add it to your project. Place the json file in a secure place, not ...

read the full post


Modal django forms with bootstrap 4

bootstrap django modals

Some time ago' I wrote an article about how to implement django forms with bootstrap 3 modals. It turned out to be a quite interesting theme since many people surfed and read that page. So now I'll refresh these concepts changing them a bit, in fact we'll see how to implement django forms with bootstrap 4-alpha modals.

Aperitif

Some preliminary stuff.

I'm almost bored with overwriting the model form class of my generic view in order to add bootstrap classes to form fields, so I'll use django-widget-tweaks. Let's install it:

(.virtualenv)$ pip install django-widget-tweaks 

Add widget_tweaks to your installed apps ...

read the full post


Serve private media with angular, DRF, CORS and token authentication

angularjs django drf rest

A long time has passed since my last post, I was fighting hard with a project learning django REST framework and angularjs, and now time has come to share some interesting knowledges.

This entry talks about serving private media from a django REST webservice, developed with django REST framework, to angularjs, in a scenario which uses a token authentication schema and CORS requests.

The problems to face

Let's see the problems we have to deal with.

  • Media with django are normally served directly by the web server, so they are out of the authentication/permission logic of the application
  • The token authentication schema needs every request to contain an header 'Authentication' part providing the token key (also when requesting files ...

read the full post


How to exclude choices of m2m raw_id_field in django admin

django programming django-admin

This procedure took a lot of effort to be understood, but now it seems quite easy.

Scenario

Imagine we need to create a recursive m2m relationship, something like a "related posts" field assigned to a Post model. It would be nice if we could exclude the current editing post from the available choices.

This is quite straightforward if we go with the default select multiple widget. In fact we only need to create a custom form class and override the queryset property of the related_posts field in the __init__ function, excluding the id of the current instance, and then assign such form class in the ModelAdmin class, see this SO question for more info.

But things become more ...

read the full post


Pretty raw_id_fields with django-salmonella and django-grappelli

django django-admin programming tips grappelli

Sometimes, when dealing with m2m relationships, we need to use the raw_id_fields property of the ModelAdmin class, in order to choose the related objects in a new page, where we can sort and filter the available items.

Without doubt the information that the widget used by django to treat such fields gives us is a bit poor. We only see the related objects's ids, it would be nicer to have the string representation of the object.

Here comes django-salmonella:

A raw_id_fields widget replacement that handles display of an object's string ...

read the full post


Add links to django admin changelist view

django django-admin programming tips

Sometimes you may need to add one or more links for each table row in your admin changelist view. There are many ways to do it, and here we'ss see a simple one. So this post is nothing more than a tip.

In my case I had to add a link opening a pdf view of the model item detail, here comes my implementation:

- app/models.py

from django.db import models
from django.core.urlresolvers import reverse
from django.utils.html import mark_safe

class MyModel(models.Model):
    title = models.CharField('title', max_length=255)
    # ...

    def pdf_link(self):
        return mark_safe('<a class="grp-button" href="%s" target="blank">view pdf</a>' % reverse('archiviocr-opera-pdf', args=[self.id]))
    pdf_link.short_description = _('PDF')

- app/admin ...

read the full post


Paginating the results of a Django form POST request

django forms pagination

This is quite a common situation, we have a search form which leads to a result page. The number of found results can be big enough to require pagination. If we use the classic pagination code, the posted parameters will be lost when changing page and our search gets broken.

There are several ways to solve the problem, as tux21b wrote in this stack overflow answer.

Here I will talk about the first method, using session.
In the same stack overflow page you can see an answer by rvnovaes, which surely works but in my opinion has some eliminable drawbacks. The first is that if there are many search fields the code we have to write is too long (yes ...

read the full post


How to implement modal popup django forms with bootstrap

django forms bootstrap

UPDATE 11/18/2015

I wrote another article like this, but newer, just tested and with support for both bootstrap 3 and 4-alpha, check it out:

http://www.abidibo.net/blog/2015/11/18/modal-django-forms-bootstrap-4/


Sometimes could be a good idea to have the user provide all the needed information without living the "index" or "main" page of our web application. In other terms, sometimes could be a good idea to have some forms appear on a layer, a modal ...

read the full post


Check if user belongs to a group in django templates

django tips

Generally when implementing some "authentication logic" inside a django template it's enough to check if a user has some permissions, and in such cases you can use the variable perms, which is available if (from django site):

Technically, these variables are only made available in the template context if you use RequestContext and your "django.contrib.auth.context_processors.auth", which is default. For more, see the RequestContext docs.

But could happen that you need to check if a user belongs ...

read the full post


Django chained selects with mootools

django forms mootools tips

Notice!

A due specification here!
Such method works, but IMHO should not be used with a large amount of data, since it uses a js mapping array, but is simple and requires a few lines of code, so why not to use it when we have a controlled set of data?

Introduction

This is just yet another method to implement chained (cascading) selects in django forms. I'm focusing here on the frontend form construction, not the admin area. If you're interested in the admin area instead, please take a look here.

Scenario

Imagine we have such models:

class ...

read the full post


Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!

Now available for purchase!

Featured