IT Blog

html_content with attachment in django-post_office

I get strange bug with attachment in django-post-office (at least, it's encountered in version 2.0.8 and lower): when the mail is sent, which consist of html-content with attachment (of any format), the mail is come, but without attachment.

I used next package version:

django==1.10.7 (and lower)
django-post-office==2.0.8 (and lower)

In more detail, the behavior of django-post-office of the specified version and the working of the mail.send() method can be described as follows:

  1. When the mail is sent with simple content, using message parameter, and with attachment - it works correctly, i. e. I get mail with attachment.
  2. When the mail...

cannot alter table because it has pending trigger events

You can see error cannot alter table because it has pending trigger events when property of your fields in database is cannot change in one transaction. To understand easy you can see examples.

Romeve blank=True in fields

So, there is model:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

class MyModel(models.Model):
    title = models.CharField(max_length=100, null=True, blank=True)
    content = models.TextField(null=True, blank=True)
    template = models.CharField(max_length=255, null=True, blank=True)

    def __str__(self):
        return self.title

So null=True is extra useless property for CharField, TextField then you need remove null=True:

class MyModel(models.Model):
    title = models.CharField(max_length=100, blank=True)...

Hide warning in PyCharm

Warning: might be referenced before assignment

Use # noinspection PyUnboundLocalVariable, for example:

def get_rate(self, rates, amount):
    for rate_item in rates:
        if amount <= rate_item.amount:
            return rate_item.rate

    # noinspection PyUnboundLocalVariable
    return rate_item.rate

Warning: unexpected argument

Use # noinspection PyArgumentList, for example:

# noinspection PyArgumentList
product = Cloth(product_name='title',product_code='xxx', unit_price=120)

Available identifiers for hiding of warning in PyCharm

In common case, write down # noinspection [warning identifier] before warning row.

# noinspection PyAbstractClass
# noinspection PyArgumentEqualDefault
# noinspection PyArgumentList  # unexpected argument
# noinspection PyAssignmentToLoopOrWithParameter
# noinspection PyAttributeOutsideInit
# noinspection PyAugmentAssignment
# noinspection PyBroadException
# noinspection PyByteLiteral
# noinspection PyCallByClass

NoReverseMatch: Reverse with arguments and keyword arguments '{}' not found. 0 pattern(s) tried: []

Periodically, I get next error:

NoReverseMatch: Reverse for 'my_url_name' with arguments '(u'...',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

I do not understand quickly why my code is not works. Well, I write down this notes. Check list:

1. Include of app to project

urlpatterns = patterns('',
    url(r'^my_app/', include('my_app.urls')),

2. Include url to of app:

urlpatterns = patterns('',
    url(r'^my-url-name/(?P<my_object_id>\d+)/$', my_app.views.my_view, name='my_url_name'),

3. Add required arguments to

urlpatterns = patterns('',
    url(r'^my-url-name/(?P<my_object_id>\d+)/$', my_app.views.my_view, name='my_url_name'),

4. Add required arguments with call 'url' function in template or 'reverse' function in...


InvalidBasesError: Cannot resolve bases for [<ModelState: 'cms.PageUser'>]

If you get this error:

django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'cms.PageUser'>]

Then the problem is in migration of django cms. This migration try to refer to migrations of User model:

class Migration(migrations.Migration):

    dependencies = [
        ('auth', '__first__'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),  # refer to migrations of User model
        ('sites', '__first__'),

And doesn't find, because there aren't migrations yet for our model of custom user. So before the creations of migrations you need comment djangocms_text_ckeditor and cms applications in

    'my_custom_auth',  # our custom User model

    # 'djangocms_text_ckeditor',
    # 'cms',

Run makemigrations:...


Changed pages in django cms

For finding of changed pages (or dirty pages) you can use Title model:

dirty_titles = Title.objects.filter(publisher_is_draft=True, publisher_state=PUBLISHER_STATE_DIRTY)

Result can displayed as like:

{% if dirty_titles %}
    <p style="font-weight: bold;">Dirty pages:</p>
    {% for title in dirty_titles %}
        <p class="changelink"><a target="_blank" href="{{ }}">{{ }}</a></p>
    {% endfor %}
{% endif %}

If you have multilanguage site, then you need provide group by languages (this property is in instance of Title class). Or you can display your result as like page structure in django cms.

I use my solution for display in information block "Notification" to notify about unpublished changes on pages.


Round in python

There is a round(number, ndigits) function for rounding of numbers, where number is required number for rounding, ndigits is a number of simbols after comma. For example:

round(2.137, 2)  # = 2.14

Round function has some feature that consists in the fact that it works on the principle of bank rounding to round off the numbers in which the last five is a sign, for example:

round(2.05, 1)  # = 2.0
round(2.15, 1)  # = 2.1
round(2.25, 1)  # = 2.3
round(2.35, 1)  # = 2.4
round(2.45, 1)  # = 2.5
round(2.55, 1)  # = 2.5
round(2.65, 1)  # =...

There is no search on this site, so I offer to use usual search engine, for example, Google, adding "vivazzi" after your request.

Try it

Select currency for displaying monetary values