Saturday, October 19, 2013

Site matching query does not exist. Lookup parameters were {'pk': 1}

Django 1.5.1

I created sample app and run the syncdb (python manage.py syncdb) and it created default tables for the application. It didn't created the tables for the app's model. For that I had to run -

python manage.py syncdb --all

It took care of the other tables creation.

Though while accessing the app I run into following error -

Traceback (most recent call last):
  File "/Users/Jaimin/Apps/Github/kqotes/quote/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in get_response
    response = middleware_method(request, response)
  File "/Users/Jaimin/Apps/Github/kqotes/quote/lib/python2.7/site-packages/django/contrib/redirects/middleware.py", line 23, in process_response
..................
  File "/Users/Jaimin/Apps/Github/kqotes/quote/lib/python2.7/site-packages/django/db/models/query.py", line 389, in get
    (self.model._meta.object_name, kwargs))
DoesNotExist: Site matching query does not exist. Lookup parameters were {'pk': 1}
Internal Server Error: /api-auth/login/

Tried different things, though couldn't figure anything.

Solution - After deleting the schema, I run python manage.py syncdb --all

bingo! All worked fine, and magically error is gone.

The issue was first time it didn't created entry for the app in site table (Its part of default tables when you do syncdb.)

select * from django_site

You can add manual entry, or drop the schema and run it again. That will assign the primary key object and app will come up fine.

Any other comments, observation, feel free to drop in comment.

Monday, October 14, 2013

install ipython notebook on mac

First why you need IPython to getting started with -

- It provides interactive capability with rich architecture
- Browser based support
- Visualization
- Save your exercise capability
- High performance tool for parallel computing and statistics analysis on datasets.

How to install ipython notebook on mac -

There are various ways you can install it, and you can find so many resources online, though I try here listing down the steps which I followed.


You can follow below instruction to install it -

$ pip install readline ipython
$ sudo pip install pyzmq
$ sudo pip install jinja2
$ sudo pip install tornado

Once all those gets installed fine, run following command to check whether it installed fine or not. You can check on command prompt first by typing ipython. 

You should be able to see on command prompt if you just run ipython -

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
.....

Once it comes up fine, start notebook

$ ipython notebook --pylab=inline

and it should open browser with -



You can click on New Notebook and try out few things to check its working fine, like 1+1, or 5*5 etc.





All Set!

Other clean recommended way to install is with proper virtualenv and requirement.txt file -


Create a seperate virtual env and run following requirement.txt

numpy==1.7.1
pandas==0.11.0
python-dateutil==2.1
pytz==2013b
pyzmq==2.1.11
six==1.3.0
wsgiref==0.1.2

And install pip install -r requirement.txt

Above instruction is for mac, which is comparatively easy compared to other environments. Feel free to post comments or issues you run into while installing.

Thursday, September 12, 2013

iter() returned non-iterator of type '_timelex'

If you running into issue -  iter() returned non-iterator of type '_timelex', here is the solution -

You got python-dateutil 2.0 with python 2.7 which is not compatible. What you need is, install older version of python-dateutil, so downgrade to python-dateutil==1.5


In order to downgrade the version you can get details over here.

Django - generate list of individual item from queryset result


How to get particular value from Django queryset result set -

Lets say you only one field 'question_id' from Paper object -

> Paper.objects.values('question_id')
>[{'question_id': 311L}, {'question_id': 310L}, {'question_id': 291L}, {'question_id': 302L}, {'question_id': 301L}, {'question_id': 300L}, {'question_id': 299L}, {'question_id': 298L}, {'question_id': 294L}, {'question_id': 293L}, {'question_id': 282L}, {'question_id': 281L}, {'question_id': 235L}, {'question_id': 234L}, {'question_id': 233L}, {'question_id': 47L}, {'question_id': 276L}, {'question_id': 48L}]

Now if you want to make list of this question_ids, one way is to loop thru the list of dict and fetch the value, but easy way is following -

>Paper.objects.values_list('question_id', flat=True)
>[311L, 310L, 291L, 302L, 301L, 300L, 299L, 298L, 294L, 293L, 282L, 281L, 235L, 234L, 233L, 47L, 276L, 48L]

Thursday, August 22, 2013

jquery event on addClass or show/hide

Its often require to fire some event when new class is getting added to the element, or when element is moving from hidden state to visible state or otherwise.

I come across similar scenario while rendering graph using jqplot library. Though if you have div hidden and you try to plot the graph on it, it won't display the graph when you make the parent variable visible. In similar situation it requires replot (which is basically redraw of the element.)

As there is no 'hide' event in DOM, so we will not be able to bind the event. Other way to resolve this issue is by Javascript MutationObserver. In which you can select target node (element) and observer instance.

Though you can find many complex solution on web, here is the simple and neat way to do it using jQuery.

Lets say you have element with class 'xyx', and upon making it to visible you would like to fire few more calls and refresh certain things on the page. You can do it using trigger -

$(".xyz").show();
$("xyz.").trigger('cssClassChanged');

The similar calss cssClassChanged needs to be listen somewhere that its been triggered, so its almost like publisher - subscriber model. Here is the sample -

$('.xyz').bind('cssClassChanged', function(){ perform your steps });


Easy and simple solution.

Monday, July 29, 2013

Setup email notification for team on github commits

Once more than one person start working on some code its good to get notification on what other team mates are committing and with git repo its easy to setup this with help of google groups.

  1. Create a new google group. Add one member - noreply@github.com
    Add the team/users email address which should be same as their github email id, the one they using it to access repo.
  2. Go to Github repository.
  3. Click on Settings from right navigation.
  4. Click on Service Hooks from left navigation.
  5. Click on Email - Provide google group name you have created (@googlegroups.com).
  6. Click Update the settings.
  7. Check its working fine by click on 'Test Hook'.

At the end you should get sample test email with last commit. After this setup all team members/members of google groups will get email upon commit from anyone on the repo.

Thursday, July 25, 2013

Django inline extend from different pages based on condition

Its been sometime I have posted anything on this blog. Recently I came across nice inline syntax if you need to extend the pages based on certain condition. e.g. I have container master page which i want to choose differently if user is logged in or not -

{% extends user.is_authenticated|yesno:"my_home.html,base_home.html" %}

It checks if person is authenticated or not, if authenticated it will extend my_home.html otherwise it will extend base_home.html