Snippet 7: adding plugins for endless-pagination, facebook login, autocomplete, and PIL

Super awesome email from my best bud Ant (shout-out here) to add several frequently used plugins for our django project. All these works to a T for me. Thanks Ant!

1) Added pagination (Django-endless-pagination)

Key feature: allow us to extract chunk by chunk from database and present it on front-end in pages. Clean display and efficient database extraction. Can possibly do the facebook endless scroll display too.

To get it go to terminal and type:

$ pip install django-endless-pagination

2) Added facebook login: (Django allauth)

To get it installed on ur local comp go to terminal and type:

$ pip install django-allauth

If that gives errors during runtime, den get addional support by going to terminal and type:

$ pip install -e git://github.com/pythonforfacebook/facebook-sdk.git#egg=facebook-sdk

If that still fails, go to settings.py and set:

USE_TZ = False

3) Added an autocomplete plugin (django selectable)

Key feature: allow autocomplete from our database, works with foreignkey, manytomany etc..

To get it installed:

$ pip install django-selectable

4) Impt restructuring – images: Add PIL

First get python imaging library (PIL) installed:

Step 1 – Get JPEG support:

$ curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz

$ tar zxvf jpegsrc.v8c.tar.gz

$ cd jpeg-8c/

$ ./configure

$ make

$ sudo make install

Step 2 – Get Freetype support:

curl -O http://ftp.igh.cnrs.fr/pub/nongnu/freetype/freetype-2.4.5.tar.gz

tar -xvzf freetype-2.4.5.tar.gz

cd freetype-2.4.5

./configure

make

sudo make install

cd ../

 

Step 3 – install PIL

$ pip install PIL

Images are now stored in the following fashion:

All images still have 1 link, for example:

Images.link = “ngo_images/HueHelp/1.jpg”   (previously “/site_media/images_beta/ngo_images/HueHelp/1.jpg”)

You can access the different sizes of the image by concatenating that to the following:

/site_media/images_beta/thumbnail/ – 60px by 60px (~1-4kb)

/site_media/images_beta/small/ – 300px by 300px (~20kb)

/site_media/images_beta/large/ – 900px by 900px (~100-200kb)

/site_media/images_beta/original/ – whatever

So for example if you are passing an ngo object, and you want to display the link to its logo:

<img class=”ngo_avatar” src=”/site_media/images_beta/small/{{ngo.logo}}” alt=”{{ngo.name}}”/>

Advertisements
Posted in Snippets | Leave a comment

Snippet 6: extending or specifying Model Instances

Refer to documentation here

More specifically for def __str__(self):

“The __str__() method is called whenever you call str() on an object. The main use for this method directly inside Django is when the repr() output of a model is displayed anywhere (for example, in debugging output). Thus, you should return a nice, human-readable string for the object’s __str__().” 

Example:

Screen Shot 2013-02-18 at 3.02.43 PM

Error: if you put an integer here it will throw a type error!

Screen Shot 2013-02-18 at 3.04.43 PM

The error would look like:

TypeError at /admin/
__str__ returned non-string (type datetime.date)

Solution is to use python’s conversion to string method  str():

Screen Shot 2013-02-18 at 3.04.59 PM

Posted in Snippets | Leave a comment

Snippet 5: Using fieldsets to customize admin page

We can use fieldsets to make admin page more organized:

1. In admin.py, under your class’s admin, create the fieldsets:
Screen Shot 2013-02-16 at 11.39.00 PM

Notice the brackets used in {  ‘fields’:[( A , B ), C , D]   }  This allows attributes grouped by () to be displayed within the same line:
Screen Shot 2013-02-16 at 11.42.40 PM

Here’s django’s documentation on fieldset

 

Posted in Snippets | Leave a comment

Snippet 4: Adding inline to admin for both ForeignKey and ManyToManyField

1. Foreign Key Method: Refer to Tutorial 4 (Part 2)

Note: using this same method for ManyToManyField will cause an error

2. ManyToManyField:

a) Under admin.py, create an inline class:
Screen Shot 2013-02-16 at 11.00.38 PM
Note: the class must be created before the admin class.. python compiles from top
Note: ‘vp’ is the variable name of the ManyToManyField in ‘Image’ that links both tables.. see the Image Class to understand:
Note: you must include ‘through’.. it tells django to use the table – that links VolProg and Image tables together – as a model to construct the inline class

Screen Shot 2013-02-16 at 11.03.54 PM
Screen Shot 2013-02-16 at 11.04.42 PM

b) of course, don’t forget the basics to import the admin, models, and register the admin
Screen Shot 2013-02-16 at 11.00.50 PM

Screen Shot 2013-02-16 at 11.00.57 PM

Here’s how it looks like:
Screen Shot 2013-02-16 at 11.42.52 PM

if you still don’t get this brief example, try this stackoverflow link

Posted in Snippets | Leave a comment

Snippet 3: Reset DB, See SQL statements, SyncDB

(python manage.py reset appName )  — reset whole database

( python manage.py sqlclear appName ) —- sneak peak DROP statements, use ( python manage,py dbshell ) to execute the drop statements

( python manage.py sql appName ) — sneak peak SQL statements to add TABLES

( python manage.py syncdb )   — sync database

Posted in Snippets | Leave a comment

Snippet 2: Dropping individual table

See this: http://stackoverflow.com/questions/2286276/how-do-i-drop-a-table-from-sqlite3-in-django

get the DROP statements with

python manage.py sqlclear <app name>

then try

python manage.py dbshell
and execute the DROP statement

And this: https://docs.djangoproject.com/en/dev/ref/django-admin/#sqlclear-appname-appname

Posted in Snippets | Leave a comment

Snippet 1: Understanding ForeignKey and M2M

Understanding ForeignKey and ManyToManyField in models.Model:

1. ForeignKey declares Many-To-1 relationship

– uses variable to access PrimaryKey

– puts ‘Many’ of itself as ‘set’ to PrimaryKey object

………………………………. \

VolProg (ForeignKey) ——- (PrimaryKey) Ngo

……………………………….. /

2. M2M declares Many-To-Many relationship

– uses variable.all to access all ‘Many’ PrimaryKeys

– puts ‘Many’ of itself as ‘set’ to PrimaryKey objects

………………………………. \ ……… /

VolProg (M2MField) ———- ———-(PrimaryKey) Ngo

……………………………….. / …….. \

Posted in Snippets | Leave a comment