Archives for 

PYTHON

error: command ‘cc’ failed with exit status 1; Can’t install PIL, Pillow, MySql and other packages in Mavericks.

If you are getting the following error when installing python packages using PIP or easy_install, don’t worry. You have come to the right place for solution.

First make sure you have developer tools installed. You can install them by executing the following command in terminal

xcode-select --install

The clang errors are due to changes introduced in Xcode 5.1 and due to the way the Apple-supplied system Python 2.7 is built. You can tell them to shut up by adding these environment variables in terminal.

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

Error while installing PIL, Pillow, MySql on OSX Mavericks using PIP

**clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
error: command 'cc' failed with exit status 1**

Djnago | Set the defulat ordering rule for queries in the models

Though we can specify order explicitly in queries like below

Posts.objects.order_by('published')

it is repetitive and most of the times we will want to order by a particular field.

We can specify the order in the models like this

class Post(models.Model):
    title = models.CharField(max_length=140)
    content = models.TextField()
    published = models.DateTimeField()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['published']

Restrict Django Templates From Deleting Data.

Django allows method calls inside template system. Though this is an useful feature, some of the methods will have side effects. Mostly the ones that alter data.
Say, for instance, you have a UserAccount object that has a delete() method. If a template includes something like {{ account.delete }}, where account is a UserAccount object, the object would be deleted when the template is rendered!

To prevent this, set the function attribute alters_data on the method:

def delete(self):
# Delete the account
delete.alters_data = True

The template system won’t execute any method marked in this way. Continuing the above example, if a template includes {{ account.delete }} and the delete() method has the alters_data=True, then the delete() method will not be executed when the template is rendered. Instead, it will fail silently.

What do Dollar and Caret ( $ and ^) sign in Django urls mean?

$ and ^ are regular expression characters that have a special meaning: the caret means “require that the pattern matches the start of the string,” and the dollar sign means “require that the pattern matches the end of the string.”

For example consider the  following url pattern

urlpatterns = patterns('',
    url(r'^hello/$', hello),
)

Without the dollar sign at the end it will match any url starting with hello like

  • hello/satish
  • hello/gandham/
  • hello/satish/123/pqr

Note: use url(r'^$', my_homepage_view), to match root.

Right way to break Django code for debugguing

Django traceback is a very helpful tool in Django development. It saves us from the boring tasking of writing print statements to track variables.

Traceback doesn’t kick when there is no error.  There may be no coding errors but there is a flaw in our application logic which we want to debug. In such cases we can trigger Django traceback by adding assert False in our code.

example:

def hello_world(response):
test_var = "Something"
test_var2 = "another var"
assert false
return HttpResponse(test_var+test_var2)

Error whil creating super user in django1.4 on MAC

I got the following error while trying to create a super user from shell for django1.4.

File "/Library/Python/2.7/site-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username
    return getpass.getuser().decode(locale.getdefaultlocale()[1])
TypeError: decode() argument 1 must be string, not None

This problem was rectified by running this command in terminal before adding the user
export LANG="en_US.UTF-8"

I’m guessing this character encoding problem while running syncdb is the reason for the following error while accessing admin section of django1.4

DoesNotExist at /admin/
Site matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.4
Exception Type: DoesNotExist
Exception Value:
Site matching query does not exist.
Exception Location: /Library/Python/2.7/site-
packages/Django-1.4-py2.7.egg/django/db/models/query.py in get, line
366
Python Executable: /usr/bin/python
Python Version: 2.7.1
Python Path:
['/Users/mohammedelsebaey/a/mysite1',
'/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/
python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/
python/PyObjC',
'/Library/Python/2.7/site-packages']

If i’m wrong you can solve the above problem by running this code i django shell

from django.contrib.sites.models import Site
Site.objects.create(pk=1)