Monday, March 30, 2015

Writing a DJango App - Part I


The example is to create a poll application. There are two parts of this application 

1. A public site that lets people view polls and vote in them 
2. An Admin site that lets admin to add polls , delete them 

To know whether Django is installed properly, below is the command that can be used 

python -c “import Django; print(django.get_version())”

If Django is installed, it will show the version number. For me, it shown as 1.9. 

Now to create a new Project, we need to run the command below 

django-admin.py startproject mysite

However, this was giving a lot of errors like below 

dministrators-MacBook-Pro-3:~ retheesh$ django-admin.py startproject mysite
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 2, in
    from django.core import management
  File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 68
    commands = {name: 'django.core' for name in find_commands(__path__[0])}
                                      ^
SyntaxError: invalid syntax

Investigating more on this, it looked like the Python version in which Django got installed is old version. 1.9 version of Django seems to require any python version greater than 3.x. Below few lines can identify in which python the django is installed. In my case, the python version that was installed from dmg file downloaded from python website was 3.4 

administrators-MacBook-Pro-3:~ retheesh$ python3.4
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named 'django'
>>> 

In the above, it says no module django found. At the same time, if run like below 

administrators-MacBook-Pro-3:~ retheesh$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> 

The above is the default system python which is in 2.7.6 and it successfully able to import django because django is installed in that python. 

Next series, need to work on installing the django on python 3.4 ! 

References: 

No comments:

Post a Comment