Solve no module name in Django

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

Posted in Django | Leave a comment

Install LXML in Debian

To install LXML python module in Debian, you need to install depedency packages :

1
apt-get install python-dev libxml2 libxml2-dev libxslt1-dev libbz2-dev libssl-dev p7zip-full rar lha unrar unzip unace unp bzip2 gzip patch

Then, you can install lxml by :

1
pip install lxml
Posted in Server | Tagged | Leave a comment

Solve slow DNS in HpCloud due to double reload on the website

I found something weird with my Bind in HPCloud which it’s working well in my EC2. After trying several experiment, I found the correct way to set bind in HPCloud. This is for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;
; BIND data file for local loopback interface
;
$TTL    3600
@       IN      SOA     obroll.com. admin.obroll.com. (
                             30        ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
obroll.com  IN  A       15.185.178.20
@       IN      NS      ns1.obroll.com.
@       IN      A       15.185.178.20
ns1     IN      A       15.185.178.20
ns2     IN      A       15.185.178.20
www     IN      A       15.185.178.20

Yes, I put my domain name as first :)

Posted in Server | Tagged | Leave a comment

Solving Bind DNS un-resolved when nslookup domain without WWW

This is common mistake when we try to nslookup our domain that working with “WWW” but not without “WWW”. This is some bind9 configuration :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
;
; BIND data file for local loopback interface
;
$TTL    3600
@       IN      SOA     obroll.com. admin.obroll.com. (
                             30        ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns1.obroll.com.
ns1     IN      A       15.185.178.20
ns2     IN      A       15.185.178.20
www     IN      A       15.185.178.20

Continue reading

Posted in Server | Tagged , | Leave a comment

Solve EnvironmentError: mysql_config not found Mysql-Python pip on Ubuntu

If you installing MySQL-python from pip in Ubuntu and see this error :

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Downloading/unpacking MySQL-python==1.2.3 (from -r requirements.txt (line 3))
  Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded
  Running setup.py egg_info for package MySQL-python
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
      File "/home/ubuntu/.virtualenvs/trip/build/MySQL-python/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 14, in <module>

  File "/home/ubuntu/.virtualenvs/trip/build/MySQL-python/setup.py", line 15, in <module>

    metadata, options = get_config()

  File "setup_posix.py", line 43, in get_config

    libs = mysql_config("libs_r")

  File "setup_posix.py", line 24, in mysql_config

    raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /home/ubuntu/.virtualenvs/trip/build/MySQL-python
Storing complete log in /home/ubuntu/.pip/pip.log
(trip) ubuntu@ubuntu:~/Tripvillas/tripvillas[yodi]$ pip install -r requirements.txt
Downloading/unpacking git+https://github.com/yuchant/django-mailer.git (from -r requirements.txt (line 11))
  Cloning https://github.com/yuchant/django-mailer.git to /tmp/pip-PUZNs4-build
  Running setup.py egg_info for package from git+https://github.com/yuchant/django-mailer.git
   
Downloading/unpacking http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz (from -r requirements.txt (line 26))
  Downloading python-dateutil-1.5.tar.gz (232Kb): 232Kb downloaded
  Running setup.py egg_info for package from http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz
   
Downloading/unpacking Django==1.3.1 (from -r requirements.txt (line 2))
  Running setup.py egg_info for package Django
   
Downloading/unpacking MySQL-python==1.2.3 (from -r requirements.txt (line 3))
  Running setup.py egg_info for package MySQL-python
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 14, in <module>
    metadata, options = get_config()

  File "setup_posix.py", line 43, in get_config

    libs = mysql_config("libs_r")

  File "setup_posix.py", line 24, in mysql_config

    raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

----------------------------------------

This mean you should install libmysqlclient-dev. On Ubuntu 11.10 Oneiric :

1
sudo apt-get install libmysqlclient-dev libmysqlclient-dev libmysqlclient16
Posted in Python module | Tagged | 1 Comment

Setup Logger in Django 1.3 to show warning & error in console development

New guys in Django commonly have a headache to see Logger configuration in settings.py. Sometimes people only want use Logger to print error / warning / debug in server development. Here are how to do that.

First, open up your settings.py and see at logger section :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

Continue reading

Posted in Django | Tagged , | 1 Comment

Example how to rsync and restart services remote server using Fabric

Fabric is great tools in Python which can ease our development. You can start installing it by :

1
sudo pip install fabric

Let create some example cases here.
1. I have remote server which have port SSH (it a must!) and use SSH-key (PEM) for login.
2. I have django apps that located in PROJECT_PATH (eg: ~/htdocs/pricedag)

So, we need create fabfile.py in PROJECT_PATH :
Continue reading

Posted in Django | Tagged , | Leave a comment

create simple example JSON REST API in Django 1.3

Creating REST JSON API in Django in pretty easy. I know several django modules like django-tastypie, django piston, etc provide easy ways to build REST API in Django, but at this time, I will show simple example.

First, we should define our urls. Eg : “/api/search/keyword”

APP/urls.py

1
2
3
4
5
6
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('price.views',
    url(r'^api/search/(?P<keyword>[a-zA-Z0-9\s\+]+)$', 'search', name='search'),
)
</keyword>

Continue reading

Posted in Django | Tagged , | 2 Comments

Python Requests Module get Json content into dictionaries

Requests Module is great python module to use urllib2 in easy ways. For instance, let we get some JSON from my Gravatar :

1
https://es.gravatar.com/bashlook.json

In case you need how to get JSON value using urllib2 :

1
2
3
4
5
6
7
8
9
10
11
import urllib2
import json

uri = "https://es.gravatar.com/bashlook.json"
opener = urllib2.urlopen(uri)

try:
    result = json.load(opener)
except ValueError, e:
    errorMessage = str(result) + ':' + str(e)
    return errorMessage

At this cases, “result” can be accessed as dictionaries, example :

1
result['entry']

Continue reading

Posted in Python module | Tagged , | Leave a comment

Delete FileField using delete(save=False) in signals post_delete Django

We can’t overwrite delete() for deleting FileField and related files in Django. As we know that :

In Django 1.3, when a model is deleted the FileField’s delete() method won’t be called. If you need cleanup of orphaned files, you’ll need to handle it yourself (for instance, with a custom management command that can be run manually or scheduled to run periodically via e.g. cron).

Continue reading

Posted in Django | Tagged , | Leave a comment