Django

MultiValueDictKeyError formset django

28 Aug , 2014  

When update data using formset and we got error “MultiValueDictKeyError” that’s high possibilites we forgot to add hidden fields in the form. Please check your formset to have hidden fields. Some example :

1
2
3
4
5
6
7
8
9
10
11
12
13
{% for form in residenceform %}
    {% for field in form.visible_fields %}
        <div class="form-group">
            {{ field.label_tag }}
            {% render_field field class+="form-control" %}
            {{ field.errors }}
        </div>
    {% endfor %}
    {% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}
{% endfor %}
{{ residenceform.management_form }}

Server

Import PostgreSQL database

27 Aug , 2014  

Simple syntax to import postgreqsl database :

1
psql -U <username> -h localhost -d <database_name> < dump_data.sql

Django

Solve UWSGI service not started in Ubuntu

27 Aug , 2014  

I found some weird bug when install uwsgi in Ubuntu 13.10 and 14.04 (after upgrade). When i run :

1
sudo service uwsgi start

No uwsgi services started and .ini are completely fine (executed with “uwsgi –ini my-app.ini”).
I found simple solution by uninstall and reinstall uwsgi packages

1
2
sudo apt-get remove --purge uwsgi uwsgi-plugin-python
sudo apt-get install uwsgi uwsgi-plugin-python

But careful, when you already did this but “service uwsgi start” result is “fail” or no uwsgi service started, it’s because www-data permission can’t write into root folder.

Please use “pidfile2” instead of “pidfile”. Here is some example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[uwsgi]
# enable master process manager
master = true

# spawn 2 uWSGI worker processes
workers = 4

# bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket
socket = /tmp/polatic.sock

# set mode of created UNIX socket
chmod-socket = 666

# place timestamps into log
log-date = true

# user identifier of uWSGI processes
uid = www-data

# group identifier of uWSGI processes
gid = www-data

; define variables to use in this script
project = polatic

; number of worker processes
processes = 2

plugins=python
; project-level logging to the logs/ folder
logto = /tmp/polatic-uwsgi.log

; django >= 1.4 project
chdir = /var/polatic
wsgi-file=/var/polatic/polatic/wsgi.py

; run master process as root
enable-threads = true
processes = 10

vacuum = true
;env = DJANGO_SETTINGS_MODULE=djtest.settings
;module = polatic.wsgi:application
env = DJANGO_SETTINGS_MODULE=vasham.settings
pidfile2 = /tmp/polatic.pid
;harakiri = 20 # respawn processes taking more than 20 seconds
;max-requests = 5000 # respawn processes after serving 5000 requests

Django

Override and Rename Django form default errors globally

18 Aug , 2014  

Here is a quick way to override django forms default errors like this field is required, etc globally :

1
2
3
4
5
# form error message override
from django.forms import Field
Field.default_error_messages = {
    'required': _("Kolom ini wajib diisi."),
}

HTML

Solve scroll in form type number bug in Chrome / Firefox

18 Aug , 2014  

Using form with type=”number” is great. But, the scroll seems annoying. Here is to avoid this issue using css :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Fix input type number scroll */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

/* hides the spin-button for firefox */
input[type=number] {
    -moz-appearance:textfield;
}
/* hides the spin-button for chrome*/
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

Django

Import 3rd party custom python modules in Django

18 Aug , 2014  

Sometimes we need to customized 3rd-party python modules and integrated with our Django. There are several options, but what i use here is the best for most cases. We should keep them separated from django and treat as python modules which we can install using “pip install”.

Here is the solution, first we need to have github or bitbucket account. Fork and modify 3rd-party modules and hosted into your account.

Some example, i’ve django-pagination-bootstrap modified. So, i put into :

1
https://bitbucket.org/polatic/django-bootstrap-pagination

In requirement.txt, we just need to put zip link :

1
https://bitbucket.org/polatic/django-bootstrap-pagination/get/master.zip

That’s it. Remember, we can do pip install on zip file.

Django

Solve no module name in Django

13 Aug , 2014  

This is trivial problem when suddenly your django page throw “no module name

1
2
    url(r'^edit_member/(?P<primary_key>w+)/$', 'members.views.create_member', name="edit_member"),
    url(r'^edit_profile/(?P<primary_key>w+)/$', 'member.views.create_profile', name="edit_profile"),

The module “member” instead of “members in last line will lead into problem