tag:blogger.com,1999:blog-85117801562631030842024-03-05T21:32:48.765-08:00Priyank PatelTypical Web Developer Blog. New Technologies, Mostly Step by Steps Guides to get it done quickly.Unknownnoreply@blogger.comBlogger28125tag:blogger.com,1999:blog-8511780156263103084.post-63492968864455141582015-07-02T05:15:00.001-07:002015-07-02T05:15:42.302-07:00Django 1.8 + GIS (postgis) + PostgreSQL 9.4 + Digital Ocean + Gunicorn + Nginx + Ubuntu 14.04 & Virtualenv. Complete Deployment Guide step by step.<div dir="ltr" style="text-align: left;" trbidi="on">
<br /><br />
<h4>
<b>Step 0 : Create droplet with django application image. </b>
</h4>
<h4>
<b>Step 1 :</b>
</h4>
<p>Connect to Digital Ocean server : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
ssh root@<ip_address>
</pre>
</div>
<h4>
<b>Step 2 :</b>
</h4>
<p>As Ubuntu 14.04 comes with postgresql 9.3, remove it completely and install 9.4 </p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo apt-get --purge remove postgresql\*
sudo aptitude update
sudo aptitude dist-upgrade
echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo aptitude update
## install 9.4
sudo aptitude install --with-recommends postgresql-9.4 postgresql-contrib-9.4 postgresql-server-dev-9.4
</pre>
</div>
<h4>
<b>Step 3 :</b>
</h4>
<p>Install pip, git , supervisor, postgresql 9.4 etc .</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo apt-get install python-pip python-dev libpq-dev postgresql-contrib git supervisor
sudo apt-get install python-imaging libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev
sudo apt-get install postgresql-9.4-postgis python-psycopg2 postgresql-9.4 postgresql-server-dev-9.4
</pre>
</div>
<h4>
<b>Step 4 :</b>
</h4>
<p>set up virual env and git repository .</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
cd /home/django/
sudo pip install virtualenv
virtualenv <prj_name>env
source <prj_name>env/bin/activate
git clone <git repo url> <prj_name>
cd <prj_name>
pip install -r requirements.txt ## make sure pip install gevent
ln -s /usr/include/freetype2 /usr/include/freetype # if you are using pillow or pip
</pre>
</div>
<h4>
<b>Step 5 :</b>
</h4>
<p>GEOS, GIS, PROJ, GDAL installation. If you are not intend to use any of this libs, skip this step.</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo apt-get install binutils libproj-dev gdal-bin
sudo apt-get install python-gdal
</pre>
</div>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
#make temp directory ourside project directory and download below contents and install. It may take more time .
## GEOS
wget http://download.osgeo.org/geos/geos-3.3.8.tar.bz2
tar xjf geos-3.3.8.tar.bz2
cd geos-3.3.8
./configure
make
sudo make install
sudo ldconfig
cd ..
## PROJ.4
wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz
wget http://download.osgeo.org/proj/proj-datumgrid-1.5.tar.gz
tar xzf proj-4.8.0.tar.gz
cd proj-4.8.0/nad
tar xzf ../../proj-datumgrid-1.5.tar.gz
cd ..
./configure
make
sudo make install
sudo ldconfig
cd ..
## GDAL
wget http://download.osgeo.org/gdal/gdal-1.9.2.tar.gz
tar xzf gdal-1.9.2.tar.gz
cd gdal-1.9.2
./configure
make
sudo make install
sudo ldconfig
cd ..
## PostGIS
sudo apt-get install libxml2-dev
wget http://download.osgeo.org/postgis/source/postgis-2.1.5.tar.gz
tar xzf postgis-2.1.5.tar.gz
cd postgis-2.1.5
./configure
make
sudo make install
sudo ldconfig
cd ..
###
</pre>
</div>
<h4>
<b>Step 6 :</b>
</h4>
<p>Allow remote postgres connection if you are going to use pgadmin like tools.</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
vim /etc/postgresql/9.4/main/postgresql.conf
listen_addresses='localhost'
to
listen_addresses='*'
vim /etc/postgresql/9.4/main/pg_hba.conf
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 md5
service postgresql restart
</pre>
</div>
<h4>
<b>Step 7 :</b>
</h4>
<p>Set up db and its postgresql password</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo su - postgres
psql
ALTER USER postgres with password <something_strong_password>;
CREATE DATABASE <db_name> WITH ENCODING='UTF8' ;
\connect <db_name>;
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION postgis_tiger_geocoder;
\q
exit
</pre>
</div>
<h4>
<b>Step 8 :</b>
</h4>
<p>Migrate Django Models and final changes to settings.py file</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
# change settings.py to prod db user password
# push all changes to live and pull @ live
python manage.py migrate auth
python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic
</pre>
</div>
<h4>
<b>Step 9 :</b>
</h4>
<p>Nginx , gunicorn, & supervisor changes</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
## if you have supervisor script for example cronjob then put its short link.
ln -s /home/django/<prj_dir_name>/jobs_supervisor.conf /etc/supervisor/conf.d/jobs_supervisor.conf
# do path changes in /etc/nginx/sites-enabled/django
# do path changes in /etc/init/gunicorn.conf
############## content of gunicorn.conf###############
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectadly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django
exec /home/django/<prj_name>env/bin/gunicorn \
--name=<prj_name> \
--pythonpath=<prj_name> \
--bind=127.0.0.1:9000 \
--config /etc/gunicorn.d/gunicorn.py \
<prj_name>.wsgi:application
</pre>
</div>
<h4>
<b>Step 10 :</b>
</h4>
<p>Above steps has deployed your django project. Note down below quick steps for quick deployment when you make code change to your project.</p>
<p>Steps are as follows : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
ssh root@<ip_address>
source /home/django/<prj_name>env/bin/activate
cd /home/django/<prj_name>
git pull origin master
python manage.py collectstatic
python manage.py migrate
cd ..
chown -R django:django <prj_name>
sudo service gunicorn restart
sudo service nginx restart
sudo supervisorctl update
/etc/init.d/supervisor force-reload
</pre>
</div>
<br />
</div>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8511780156263103084.post-64421284305770167112015-06-15T02:40:00.000-07:002015-06-15T02:40:57.756-07:00Docker based Python Project Deployment with NginX, Supervisor, uwsgi on Ubuntu<div dir="ltr" style="text-align: left;" trbidi="on">
For Docker installation refer : <a href="https://docs.docker.com/installation/ubuntulinux/">https://docs.docker.com/installation/ubuntulinux/</a>
<br /><br />
<h4 style="text-align: left;">
<span style="font-weight: normal;">Assume your python code is inside /var/app/project/ and server run file is start.py </span></h4><br /><br />
<h4 style="text-align: left;">
<b>Note : Create folder/path if don't exists. for example /var/app/run/ which don't exits then create it. similarly for other paths.<br /></b><br />
</h4>
<h4>
<b>Step 1 :</b>
</h4>
<p>Create one custom supervisord.conf file inside /var/app/supervisor/</p>
<p>Content of file should be : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
[unix_http_server]
file=/var/app/run/supervisor.sock
[supervisord]
logfile=/var/app/log/supervisord.log
loglevel=info
pidfile=/var/app/run/supervisord.pid
nodaemon=true
childlogdir=/lib/app/log/supervisor
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/app/run/supervisor.sock
[include]
files = /var/app/supervisor/conf.d/*.conf
</pre>
</div>
<h4>
<b>Step 2 :</b>
</h4>
<p>Create "superisor.docker.conf" file inside /var/app/supervisor/conf.d/ </p>
<p>Content of file should be :</p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
[program:pyenv]
command = /usr/bin/docker run --rm --name pyenv -v /var/app:/var/app felixonmars/archlinux supervisord -c /var/app/supervisor/supervisord.conf
</pre>
</div>
<h4>
<b>Step 3 :</b>
</h4>
<p>make link of file /var/app/supervisor/conf.d/superisor.docker.conf in /etc/supervisor/conf.d/ using :</p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo ln -s /var/app/supervisor/conf.d/superisor.docker.conf /etc/supervisor/conf.d/
</pre>
</div>
<h4>
<b>Step 4 :</b>
</h4>
<p>Now onwards , your all supervisor .conf files will be inside /var/app/supervisor/conf.d/
</p>
<p>Create project_supervisor_docker.conf file inside /var/app/supervisor/conf.d/ and its content should be : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
[program:project]
command = /usr/bin/uwsgi --plugin=python2
--limit-as=512 --processes=2 --max-request=2000
--memory-report --enable-threads
--socket=/var/app/run/uwsgi/project.socket
--stats=/var/app/run/uwsgi/project.stats
--logto=/var/app/log/uwsgi/project.log
--pidfile=/var/app/run/uwsgi/project.pid
--master --no-orphans --logdate --chmod-socket=660
--uid=33 --gid=33 --need-app
--wsgi-file=/var/app/project/start.py
stopsignal=INT
</pre>
</div>
<h4>
<b>Step 5 :</b>
</h4>
<p>Create project_master_supervisor_docker.conf inside /var/app/supervisor/conf.d/ and its content : </p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
[program:project_master]
command = /usr/bin/python2 start.py
directory=/var/app/project
</pre>
</div>
<h4>
<b>Step 6 :</b>
</h4>
<p>Create Nginx Configuration file for project. Its content should be :</p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
upstream push_daemon_project {
server 127.0.0.1:8284; #Assume push socket on port 8284
}
server {
listen 80;
listen [::]:80;
server_name project.com www.project.com;
location = /nginx_stub_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
location / {
return 301 https://project.com$request_uri;
}
}
server {
listen 443 ssl spdy;
listen [::]:443 ssl spdy;
ssl_certificate /var/app/cert/project.com.crt; # certificate ssl
ssl_certificate_key /var/app/cert/project.com.key; # certificate key ssl
server_name project.com www.project.com;
access_log /var/log/nginx/project.access.log;
error_log /var/log/nginx/project.error.log;
root /var/app/project/web; ## template (html) directory
location /static {
alias /var/app/project/static; ## static files
}
location = /nginx_stub_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
location /websocket {
proxy_pass http://push_daemon_project;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
include uwsgi_params;
uwsgi_pass unix:///var/app/run/uwsgi/project.socket;
}
}
</pre>
</div>
<h4>
<b>Step 7 :</b>
</h4>
<p>Restart all instances :</p>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo chown -R www-data:www-data /var/app/project
sudo service nginx restart
sudo supervisorctl update
sudo /etc/init.d/supervisor force-reload
sudo docker ps -a # you can see docker process id : for ex : 6a2dfdeb0000
sudo docker restart 6a2dfdeb0000
</pre>
</div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-82015905476846374262015-06-15T02:21:00.002-07:002015-06-15T02:21:38.055-07:00Grunt , bower, nodejs installation & commands<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo apt-get install --reinstall --install-recommends nodejs
sudo npm install -g grunt
sudo npm install -g grunt-cli
sudo npm install -g bower
# Locate package.json and run :
sudo npm install
# Locate bower.json and run :
bower install
# Locate .Gruntfile.js and run :
"grunt dev --force" in local
"grunt build --force" in live
</pre>
</div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-87904239749246015832015-06-15T02:20:00.000-07:002015-06-15T02:20:01.605-07:00* Commands * Git Submodule , merge , git rm reset / undo<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
git clone <SSH-URL> <directory_name>
git submodule init
git submodule update
# merge branch in master
git checkout master
git merge <branch_name>
# Undo "git rm -r ." command.
git reset HEAD
</pre>
</div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-56955815722660963292015-06-15T02:09:00.001-07:002015-06-15T02:09:28.248-07:00Mongo Useful Commands and steps for Dump , Restore from live with ssh access ( without FTP )<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
# Run in live server
sudo mongodump --host <host_url> --port 27017 --db <db_name>
sudo tar -zcvf <compress_file_name_of_dump>.tar.gz dump
# Note down path like
/var/app/dump/<compress_file_name_of_dump>.tar.gz
scp <server_user>@<server_ip_address>:/var/app/dump/<compress_file_name_of_dump>.tar.gz <local_absolute_path_where_dump_will_download>
mongorestore --host localhost --db <db_name> --port 27017 <downloaded_dump_folder_absolute_path>
</pre>
</div>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<b>If you are not able to connect mongo server ...</b>
<br />
<pre>
sudo service mongodb stop
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
</pre>
</div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-17780927089721836272012-06-10T22:20:00.001-07:002012-06-11T00:18:13.877-07:00Django Site in Production with Lighttpd & FastCGI<div dir="ltr" style="text-align: left;" trbidi="on">
<p>OS : Ubuntu</p>
</p>Python : 2.7.1</p>
<b>1) Install lighttpd </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
sudo apt-get, install lighttpd
</pre>
</div>
<b>2) Install django<br/>
I want to download it to / var / www installation, </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
$ Cd / var / www
$ Sudo wget http://media.djangoproject.com/releases/1.3/Django-1.3.1.tar.gz
$ Sudo tar xzvf Django-1.3.1.tar.gz
$ Cd the Django-1.3.1
$ Sudo python setup.py install
</pre>
</div>
<b>3) Installed back to / www </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
cd / var / www
</pre>
</div>
<b>4) Then we have to check the django whether the installation is successful, we first build a new project with django-admin.py </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
the sudo django-admin.py startproject the portal
the cd the portal
sudo the chmod + the x the manage.py
sudo python manage.py runserver
</pre>
</div>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<span>Displays the following screen, django has a normal installation </span>
<pre>
Validating the models ...
0 errors found
Django version 1.3.1, using settings 'portal.settings'
Development server is running at http://127.0.0.1:8000/ with
Quit the server with CONTROL-C.
</pre>
</div>
<b>5) install flup </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
This is the WSGI Server, before installation, you must first determine the version of the python in the command line input
<pre>
$ / Var / www / portal $ python
Python 2.7.1 + (r271: 86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2 to
</pre>
</div>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
Ubuntu 11.04 Built-in 2.7.1 version? or because I just have the update?? XD
So we had to select the development flup-1.0.3 version
<pre>
$ Cd / var / www
$ Sudo wget http://pypi.python.org/packages/2.7/f/flup/flup-1.0.3.dev_20110405-py2.7.egg # md5 =
1fa9a03ade17f88990340884a1113b5a
$ Sudo wget http://peak.telecommunity.com/dist/ez_setup.py
$ Sudo python ez_setup.py flup-1.0.3.dev_20110405-py2.7.egg
</pre>
</div>
<b>6)by adding mysite.fcgi </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
This code is used to tell the server how to deal with the fastcgi program
<pre>
#! / Usr / bin / python
import sys, os
# Add a custom Python path.
sys.path.insert (0, / var / www ")
# Switch to the directory of your project. (Optional.)
# Os.chdir ("/ home / user / myproject")
# Set the the DJANGO_SETTINGS_MODULE environment variable.
os.environ ['DJANGO_SETTINGS_MODULE'] = "portal.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi<span style='color:red;'> (["method = threaded", "daemonize = false"])</span>
</pre>
</div>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
Above the red part, be especially careful if you look at the django official website files are written is not the same as the official website of<br/>
the paper was wrong, or is the older version of the file XD. I later installed a long time, only to find that there are wrong installation should pay <br/>
special attention to Oh!
Storage of the above file, enter
<pre>
$ For python mysite.fcgi
</pre>
There will be a long list of things. Find what you see <h1> It worked! </h1> This script file is correct.
</div>
<b>7) edit the lighttpd config </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
Start the fastcgi mode
<pre>
$ Sudo lighttpd-enable-mod Fastcgi
</pre>
</div>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
Then here to note in the / etc / lighttpd / you will see to lighttpd.conf this profile do not move, which is the default. lighttpd read this default will<br/> go to read the folder conf-enabled profile without charge. The start fastcgi mode, so read the default<br/> lighttpd would automatically go read the conf-enabled inside the profile.
Set fastcgi
<pre>
$ Vim / etc/lighttpd/conf-enabled/10-fastcgi.conf server.modules + = ("mod_fastcgi",
"Mod_accesslog
)
server.modules + = ("mod_rewrite",
Mod_fastcgi under "
"Mod_accesslog
)
$ SERVER ["socket"] == "172.16.33.10:8000" {
server.document-root = "/ var / www / portal"
the fastcgi.server = (
Fcgi "=> (
"Localhost" => (
The bin-path = "/ var / www / portal / mysite.fcgi
"Socket" => "/ var / www / portal / mysite.sock",
"Check-local" => "the disable"
)
),
)
alias.url = (
"/ Media" => "/ usr/local/lib/python2.7/dist-packages/django/contrib/admin/media /",
)
url.rewrite-once = (
"^ (/ Media. *) $" => "$ 1"
^ / The favicon.ico $ "=>" / media / the favicon.ico
"^ (/ *) $" => "/ Mysite.fcgi $ 1"
)
}
</pre>
</div>
<b>8)Establish runfastcgi the script </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
This script is mainly through the manage.py the command the specified runfcgi parameter operation, this script will be related to things in<br/> order to facilitate the start Fastcgi the server into a script.
<pre>
#! / Bin / bash
# Replace these three settings.
PROJDIR = "/ var / www / portal"
PIDFILE = "$ PROJDIR / mysite.pid"
SOCKET = "$ PROJDIR / mysite.sock"
cd $ PROJDIR
if [-f $ PIDFILE]; then
kill `cat - $ PIDFILE`
rm-f - $ PIDFILE
fi
the exec / usr / bin / env - \
PYTHONPATH is = ".. / Python: .." \
. / Manage.py runfcgi socket = $ SOCKET pidfile = $ PIDFILE
</pre>
</div>
<b>9)To remember to to modify runfastcgi execute permissions. </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
$ Sudo chmod + x runfastcgi
</pre>
</div>
<b>10)Restart lighttpd </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
$ / Etc / init.d / the lighttpd
</pre>
</div>
<b>11 ) implementation of runfastcgi </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
$. / Runfastcgi
</pre>
</div>
<b>12 )enter the URL in the browser http:// [yourIP]: 8000 </b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
You will able to see start page of your django site.
</div>
<b>13 )File permissions</b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
drwxr--xr-x 2 www-data root 4096 2011-09-22 19:42 the portal
portal folder inside the file are as follows:
- Rw-r - r - 1 www-data root 0 2011-09-22 19:16 __ init__.py
-Rw-r - r - 1 www-data root 124 2011-09-22 19:17 __ init__.pyc
-Rwxrwxrwx 1 www-data root 503 2011-09-22 19:16 the manage.py
-Rwxrwxrwx 1 www-data root 401 2011-09-22 7:22 p.m. mysite.fcgi
-Rwxrwxrwx 1 www-data root 334 2011-09-22 19:31 runfastcgi
-Rw-r - r - 1 www-data root 5031 2011-09-22 19:16 your settings.py
-Rw-r - r - 1 www-data root 2652 2011-09-22 19:17 settings.pyc
-Rw-r - r - 1 www-data root 565 2011-09-22 19:16 The urls.py
-Rw-r - r - 1 www-data www-data 261 2011-09-22 19:42 urls.pyc
</pre>
</div>
<b>13 )use the django when the Admin can not find staic resource (CSS, image, js )?
</b>
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
Modify the lighttpd config.
alias.url = ("/ static / admin /" => "/ usr/local/lib/python2.7/dist-packages/django/contrib/admin/media /")
</div>
<br/>
[Reference] <br/>
<a href="http://blog.finalevil.com/2011/09/how-to-use-django-on-lighttpd-with.html">http://blog.finalevil.com/2011/09/how-to-use-django-on-lighttpd-with.html</a>
<br/>
</div>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8511780156263103084.post-4882848252549208652012-06-03T22:46:00.001-07:002012-06-03T23:05:56.028-07:00Django 1.3 Language Translation<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="container" id="page">
<div id="content">
<div class="notphone" id="minmaxlink">
<br /></div>
<div class="post">
<h1>
Translating Templates with Django</h1>
<div class="date">
</div>
<div class="entry">
Below is steps for translating one Django websites into Norwegian. <br />
<h3>
Settings</h3>
As I'm only providing translation for Norwegian, I set the following in my settings.py file:<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre><span class="n">LANGUAGE_CODE</span> <span class="o">=</span> <span class="s">'en-gb'</span>
<span class="n">LANGUAGES</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="s">'nb'</span><span class="p">,</span> <span class="s">'Norwegian Bokmal'</span><span class="p">),</span>
<span class="p">(</span><span class="s">'nn'</span><span class="p">,</span> <span class="s">'Norwegian Nynorsk'</span><span class="p">),</span>
<span class="p">(</span><span class="s">'en-gb'</span><span class="p">,</span> <span class="s">'English'</span><span class="p">),</span>
<span class="p">)</span>
</pre>
</div>
This defaults to English, but limits the list of languages on offer. I also added <b><code>LocaleMiddleware</code></b> to my <b><code>MIDDLEWARE_CLASSES</code></b>:<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre><span class="n">MIDDLEWARE_CLASSES</span> <span class="o">=</span> <span class="p">(</span>
<span class="s">'django.middleware.csrf.CsrfViewMiddleware'</span><span class="p">,</span>
<span class="s">'django.contrib.sessions.middleware.SessionMiddleware'</span><span class="p">,</span>
<span class="s">'django.middleware.locale.LocaleMiddleware'</span><span class="p">,</span>
<span class="o">...</span>
<span class="p">)</span>
</pre>
</div>
<h3>
Mark Up Template</h3>
Open up the template to translate, and add the trans tag around the text you want translating. So,<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre><span class="nt"><h2></span>Part 9 - Three Toms and Rolls<span class="nt"></h2></span>
<span class="nt"><p></span>This section contains the first exercises containing snare drum rolls.<span class="nt"></p></span>
<span class="nt"><h2></span>Part 10 - Ride Cymbal and Cut Common<span class="nt"></h2></span>
<span class="nt"><p></span>This part introduces the ride cymbal to the drum kit<span class="nt"></p></span>
</pre>
</div>
becomes<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre><span class="nt"><h2></span>{% trans "Part" %} 9 - {% trans "Three Toms and Rolls" %}<span class="nt"></h2></span>
<span class="nt"><p></span>{% trans "This section contains the first exercises containing snare drum rolls." %}<span class="nt"></p></span>
<span class="nt"><h2></span>{% trans "Part" %} 10 - {% trans "Ride Cymbal and Cut Common" %}<span class="nt"></h2></span>
<span class="nt"><p></span>{% trans "This part introduces the ride cymbal to the drum kit" %}<span class="nt"></p></span>
</pre>
</div>
Add <b><code>{% load i18n %}</code></b> at the top of the template too.<br />
<h3>
Run make-messages.py</h3>
Once this is done, you need to run a script which will create message files. You need to run <b><code>dango-admin.py makemessages</code></b> in the same directory as your settings file, after creating a <b><code>locale</code></b> directory<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>me@coderpriyu:~/web/progperc/site$ export PYTHONPATH=~/web/site
me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py makemessages -l nb
Error: This script should be run from the Django SVN tree or your project or app tree. If you did indeed
run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale
(in the django tree) or locale (for project and application) directory? It is not created automatically, you
have to create it by hand if you want to enable i18n for your project or application.
</pre>
</div>
This error means that we need to create a <b><code>locale</code></b> directory in the directory that contains settings.py:<br />
<div class="codehilite" style="display: block; width: 565px;">
<pre>me@coderpriyu:~/web/progperc/site$ mkdir locale
me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py makemessages -l nb
processing language nb
Error: errors happened while running xgettext on coverage.py
/bin/sh: xgettext: not found
</pre>
</div>
This error occurs because we don't have the <b><code>xgettext</code></b> command installed. I'm on Ubuntu 11.04, so <b><code>sudo apt-get install gettext</code></b> should do the trick.<br />
<br />
Success!:<br />
<div class="codehilite" style="display: block; width: 565px;">
<pre>me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py makemessages -l nb
processing language nb
</pre>
</div>
I now have a file, in <b><code>~/web/site/locale/nb/LC_MESSAGES</code></b> called <b><code>django.po</code></b>, with contents similar to the following:<br />
<div class="codehilite" style="display: block; width: 565px;">
<pre># SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-06-17 11:10+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: volume/templates/volume/Volume2.html:8
#: volume/templates/volume/Volume2.html:18
msgid "Part"
msgstr ""
#: volume/templates/volume/Volume2.html:8
msgid "Three Toms and Rolls"
msgstr ""
#: volume/templates/volume/Volume2.html:9
msgid ""
"This section contains the first exercises containing snare drum rolls. "
msgstr ""
#: volume/templates/volume/Volume2.html:18
msgid "Ride Cymbal and Cut Common"
msgstr ""
#: volume/templates/volume/Volume2.html:19
msgid ""
"This part introduces the ride cymbal to the drum kit."
msgstr ""
</pre>
</div>
<h3>
Add translation</h3>
Add your translation into the msgstr section (Warning! Google translate Norwegian ahead!)<br />
<div class="codehilite" style="display: block; width: 565px;">
<pre>#: volume/templates/volume/Volume2.html:8
msgid "Three Toms and Rolls"
msgstr "Tre Toms og Rolls"
#: volume/templates/volume/Volume2.html:9
msgid ""
"This section contains the first exercises containing snare drum rolls. "
msgstr "Denne delen inneholder de første øvelsene inneholder skarptromme ruller."
#: volume/templates/volume/Volume2.html:18
msgid "Ride Cymbal and Cut Common"
msgstr "Ride Cymbal og Cut Common"
#: volume/templates/volume/Volume2.html:19
msgid ""
"This part introduces the ride cymbal to the drum kit."
msgstr "Denne delen introduserer ride cymbal til trommesett."
</pre>
</div>
<h3>
Compile Language File</h3>
Once we've put our translations in, we need to compile the <b><code>.po</code></b> file into a <b><code>.mo</code></b> file.<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>me@coderpriyu:~/web/progperc/site$ ~/web/progperc/django/bin/django-admin.py compilemessages
processing file django.po in /home/coderpriyu/web/site/locale/nb/LC_MESSAGES
</pre>
</div>
You should now be able to run up your app, change your locale in the browser, and see the translated text.</div>
</div>
</div>
</div>
</div>
</div>Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-8511780156263103084.post-25578350441166217232012-04-17T22:37:00.000-07:002012-06-03T23:17:49.519-07:00Django Initial data dumping and loading<div dir="ltr" style="text-align: left;" trbidi="on">
<b>"manage.py dumpdata" for dumping data to json file.</b><br /><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
$./ manage.py dumpdata app.myModel1 app.myModel2 <relative_path_to_app>/fixtures/initial_data.json
--indent 4
</pre>
</div>
<br />
<b>"manage.py loaddata" for loading data from json file.</b><br /><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
$ ./manage.py loaddata <relative_path_to_app>/fixtures/initial_data.json
</pre>
</div>
</div>
<br />Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8511780156263103084.post-88822022269157316672012-04-04T00:20:00.000-07:002012-04-04T00:20:54.657-07:00SVN vs GIT<div dir="ltr" style="text-align: left;" trbidi="on">
<div id="left-container2">
<div class="bleft">
</div>
<div class="bmid">
In the world of programming and engineering, it is necessary to keep track of changes during the application development process. This allows developers and programmers revert unwanted changes in applications under the development process. </div>
<div class="bmid">
<div class="single_txtcon_block">
<br />
Version Control Systems (VCS) are stand-alone web 2.0 applications that have made the job easier by providing systemized management of multiple revisions of the same unit of information, whether it is a simple document or a digital document, such as source codes of applications or blueprints of electronic models. VCS allows developers to work collaboratively on the same application from different locations using repositories.<br />
<br />
The two most famous types of version control systems are:
<br />
1- Centralized version control system - subversion (SVN)
<br />
2- Distributed or decentralized version control system - Git<br />
<br />
In the case of <b>centralized version control systems</b>, there is a single central repository and all of the changes that are made to the documents are saved in that repository. There is a client server approach in the case of CVCS, where a single repository is stored on the server that clients can also sync up to.
<br />
<br />
In case of <b>distributed or decentralized VCS</b>, there is a peer-to-peer approach that clients can synchronize with by exchanging patches from peer to peer. Clients can make changes in the repositories and those changes will be local to them unless they synchronize with someone else. Depending on the requirements, Git also offers a centralized repository. In other terms, each peer has a bona-fide repository which is the working copy of codebase. DVCS provides independent as well as canonical repository.
<br />
<br />
<b>SVN vs. Git</b>
<br />
Both VCS and DVCS are highly appreciated by their users, but I have found that DVCS has some advantages over VCS. Let’s look at the concept of VCS and DVCS in detail by comparing the available systems of both types – SVN and Git.
<br />
<br />
<ul>
<li>With Git, clients can commit changes to their localized repositories as new revisions while being offline. However, SVN does not provide this facility as user must be online in order to push to the repository from the working copy. </li>
<li>SVN help is more organized and to the point while Git provides more than what is actually required. There is some time wasted since it is difficult to get a quick reference from Git’s search. </li>
<li>Git’s complete copy of the data is stored locally in the client’s system so it is extremely fast when compared to SVN. With Git there is no time wasted when waiting for network response time, but with SVN it takes longer because all of the data is stored in a centralized repository. </li>
<li>There is a smaller chance of data being lost in Git because data copies are stored locally in clients systems. The number of backups available is the same as the number of users on any repository. With SVN, if there is a data loss in the central repository, it will be gone forever. </li>
<li>The Git repository has an efficient memory because the data’s file format is compressed, which is not the case with SVN. Also, there are always two copies of a file in the working directory of SVN. One copy is used for storing the actual work while the other copy contains the information used to aid operations (status and commit). Git has a small index file to store the info related to a particular file. When there are a lot of documents, there is a huge impact on disk space in the SVN compared with Git. </li>
<li>Git DVCS is based on the concept of branching. The working directory of a developer is itself a branch. In Git, we can easily view the working directories of developers while they are modifying two or more unrelated files at the same time as different branches stemming from the same common base revision of the project. With SVN, there is almost no concept of branching </li>
<li>In Git a large number of users can commit or push data to the same repository. If someone wants to push work in a Git repository, then there is no need to worry about data lost or immediate merging of others changes because commits are not sequential in Git like SVN. </li>
<li>Git allows its users to have control over the merging of data in synchronized repositories. Merges are always pulled by someone and nobody can push to commit merges in someone else’s repository. The facility to merge data is also there in SVN, but it is somewhat incomplete. SVN merge records seem to miss some of the important details that Git keeps track of.</li>
<li>Git keeps track of contents while SVN keeps record of files. Because Git keeps track of contents, whenever there is even a small change in content it tracks it as a separate change. Because of this, the history of a single file in Git is split. </li>
<li>Git will not allow you to checkout a subdirectory. Instead, the user will have to checkout the whole repository. In SVN, checkouts at subdirectory level are possible.</li>
</ul>
<br />
By looking at an overview of the features of Git and SVN, we can see that Git is preferable in most circumstances. Developers, researchers, engineers and other users of VCS have more inclination towards Git, so I predict that Git will be the future of revision control.
<br />
<br />
<b>References:</b>
<br />
“Git's Major Features over Subversion” retrieved Dec 15, 2008 from “<a href="http://git.or.cz/gitwiki/GitSvnComparsion" target="_blank">http://git.or.cz/gitwiki/GitSvnComparsion</a>”</div>
<br />
<br /></div>
<div class="bright">
</div>
</div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-7575108728702105412012-04-03T23:46:00.000-07:002012-06-03T23:35:27.096-07:00Virtual Environment for Django and python with version specified.<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="post">
Python2.6 is the default Python version on Ubuntu 10.04. Now you may still want to run your Django websites with Python2.5. A nice way to do this is by creating virtual environments to handle $PYTHONPATH and avoid conflicts among different versions. Such a tool already exists: <b>Virtualenvwrapper.</b><br />
<br />
First, to install Virtualenvwrapper, we will need easy_install:<br />
<div class="codeblock">
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nv">$ </span>sudo apt-get install python-setuptools
<span class="nv">$ </span>sudo easy_install virtualenv
<span class="nv">$ </span>sudo easy_install virtualenvwrapper
<span class="nv">$ </span>mkdir ~/.virtualenvs</pre>
</div>
</div>
We just need to add the following lines to .bashrc.<br />
<div class="codeblock">
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nb">export </span><span class="nv">WORKON_HOME</span><span class="o">=</span><span class="nv">$HOME</span>/.virtualenvs
<span class="nb">source</span> /usr/local/bin/virtualenvwrapper.sh
</pre>
</div>
</div>
And run it.<br />
<div class="codeblock">
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nv">$ </span><span class="nb">source</span> .bashrc</pre>
</div>
</div>
We then create a <i>virtual environment</i>, here named <i>django</i> with Python2.5. Python2.6 is the default version on Ubuntu 10.04.<br />
<br />
<b>To create virtual environment </b><br />
<i>here -p specifies the package</i><br />
<div class="codeblock">
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nv">$ </span>mkvirtualenv -p python2.5 django
</pre>
<pre><span class="nv"><b>To use virtual environment named "django"</b></span></pre>
<pre><i><span class="nv"> bracket contains the current virtual environment</span></i></pre>
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nv">$ </span>workon django</pre>
</div>
<pre><span class="nv"><b>To close virtual environment named "django"</b></span></pre>
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre>
<span class="nv">$ </span><span class="o">(</span>django<span class="o">)</span>...:<span class="nv">$ </span>deactivate
</pre>
</div>
</div>
</div>
We are ready to go with Python 2.5. <i>workon</i> sets the current virtual environment and <i>deactivate</i> exits from it.<br />
<br />
<b>We can now add some additional Python paths on our virtual environment, for example:</b><br />
<div class="codeblock">
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nv">$ </span>workon django
<span class="nv">$ </span><span class="o">(</span>django<span class="o">)</span>...:<span class="nv">$ </span>add2virtualenv /home/user/additional_python_path
<span class="nv">$ </span><span class="o">(</span>django<span class="o">)</span>...:<span class="nv">$ </span>add2virtualenv
Usage: add2virtualenv dir <span class="o">[</span>dir ...<span class="o">]</span>
Existing paths:
home/user/additional_python_path
<span class="nv">$ </span><span class="o">(</span>django<span class="o">)</span>...:<span class="err">$</span>
</pre>
</div>
</div>
Finally here are some other useful commands:<br />
<div class="codeblock">
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre><span class="nv">$ </span>mkvirtualenv django2
<span class="nv">$ </span>rmvirtualenv django2
<span class="nv">$ </span>workon django
<span class="nv">$ </span><span class="o">(</span>django<span class="o">)</span>...:<span class="nv">$ </span>deactivate
<span class="nv">$ </span>workon
django
</pre>
</div>
<pre><span style="font-size: large;"><b>Now The most important thing is "How to use virtualenv in</b></span></pre>
<pre><span style="font-size: large;"><b>production ???"</b></span></pre>
<pre></pre>
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre>For that you will need to add it's path to your PYTHONPATH.</pre>
</div>
For example if your env had for path "/home/www/my_project/env/", the path to add would be:
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre>
<code><span class="str">/home/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">env</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">python2</span><span class="pun">.</span><span class="lit">7</span><span class="pun">/</span><span class="pln">site</span><span class="pun">-</span><span class="pln">packages</span><span class="pun">/</span><span class="pln"><br /></span></code>
</pre>
</div>
You can set this up in many different ways, but if your are
generating your fcgi or uwsgi interface through manage.py, simply add
the following at the very top of your manage.py (before the rest):</div>
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre>
<code><span class="kwd">import</span><span class="pln"> os<br />my_virtualenv_path </span><span class="pun">=</span><span class="pln"> </span><span class="str">"/home/www/my_project/env/lib/python2.7/site-packages/"</span><span class="pln"><br /></span><span class="com"># Add it to your PYTHONPATH</span><span class="pln"><br />os</span><span class="pun">.</span><span class="pln">path</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">my_virtualenv_path</span><span class="pun">)</span><span class="pln"><br /></span></code>
</pre>
</div>
You can adopt this to whatever your setup is, just in case you could also do the following in the shell :
<div style="display: block; overflow: hidden; width: 565px;" class="codehilite">
<pre>
<code><span class="kwd">export</span><span class="pln"> PYTHONPATH</span><span class="pun">:</span><span class="pln">$PYTHONPATH</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">my_project</span><span class="pun">/</span><span class="pln">env</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">python2</span><span class="pun">.</span><span class="lit">7</span><span class="pun">/</span><span class="pln">site</span><span class="pun">-</span><span class="pln">packages</span><span class="pun">/</span></code></div>
</pre>
</div>
<div class="highlight">
You will also need to add the directory of where your settings.py
file is located to the PYTHONPATH, so django will be able to discover
it, just proceed in a similar manner to do so.<br />
<br /></div>
</div>Unknownnoreply@blogger.com6Ahmedabad, Gujarat, India23.0395677 72.566004522.9226712 72.408076000000008 23.1564642 72.723933tag:blogger.com,1999:blog-8511780156263103084.post-68915864407161552642011-12-20T03:10:00.000-08:002012-05-13T23:42:45.212-07:00Scribd documents on Your page ( API ) - Django / python<div dir="ltr" style="text-align: left;" trbidi="on">
<style type="text/css">
.highlight{ background: #f2f2f2; }
.highlight .c { color: #008800; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #AA22FF; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .cm { color: #008800; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #008800 } /* Comment.Preproc */
.highlight .c1 { color: #008800; font-style: italic } /* Comment.Single */
.highlight .cs { color: #008800; font-weight: bold } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #808080 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
.highlight .kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */
.highlight .kp { color: #AA22FF } /* Keyword.Pseudo */
.highlight .kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #666666 } /* Literal.Number */
.highlight .s { color: #BB4444 } /* Literal.String */
.highlight .na { color: #BB4444 } /* Name.Attribute */
.highlight .nb { color: #AA22FF } /* Name.Builtin */
.highlight .nc { color: #0000FF } /* Name.Class */
.highlight .no { color: #880000 } /* Name.Constant */
.highlight .nd { color: #AA22FF } /* Name.Decorator */
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #00A000 } /* Name.Function */
.highlight .nl { color: #A0A000 } /* Name.Label */
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #B8860B } /* Name.Variable */
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight .mf { color: #666666 } /* Literal.Number.Float */
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
.highlight .sb { color: #BB4444 } /* Literal.String.Backtick */
.highlight .sc { color: #BB4444 } /* Literal.String.Char */
.highlight .sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #BB4444 } /* Literal.String.Double */
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #BB4444 } /* Literal.String.Heredoc */
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight .sx { color: #008000 } /* Literal.String.Other */
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight .s1 { color: #BB4444 } /* Literal.String.Single */
.highlight .ss { color: #B8860B } /* Literal.String.Symbol */
.highlight .bp { color: #AA22FF } /* Name.Builtin.Pseudo */
.highlight .vc { color: #B8860B } /* Name.Variable.Class */
.highlight .vg { color: #B8860B } /* Name.Variable.Global */
.highlight .vi { color: #B8860B } /* Name.Variable.Instance */
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
.highlight span:hover { background: #ff9 }
</style>
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
In Django to display your Scribd documents in your page , first you need to install scribd python package . To install it refer <a href="http://code.google.com/p/python-scribd/wiki/GettingStarted" target="_blank">this link</a> <br />
<br />
after installing it in your views write this def (add url accordingly).<br />
<div class="highlight" ><pre><span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">scribd</span>(request):
extra_context <span style="color: #666666">=</span> {}
extra_context[<span style="color: #4070a0">'formName'</span>] <span style="color: #666666">=</span> <span style="color: #4070a0">'scribd'</span>
data <span style="color: #666666">=</span> []
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">scribd</span>
scribd<span style="color: #666666">.</span>api_key <span style="color: #666666">=</span> <span style="color: #4070a0">'#Your api key'</span>
scribd<span style="color: #666666">.</span>api_secret <span style="color: #666666">=</span> <span style="color: #4070a0">'#Your secret key'</span>
api_user <span style="color: #666666">=</span> scribd<span style="color: #666666">.</span>api_user
all_documents <span style="color: #666666">=</span> api_user<span style="color: #666666">.</span>all()
<span style="color: #007020; font-weight: bold">for</span> ad <span style="color: #007020; font-weight: bold">in</span> all_documents:
temp <span style="color: #666666">=</span> []
document <span style="color: #666666">=</span> ad
document<span style="color: #666666">.</span>load()
attrsDict <span style="color: #666666">=</span> document<span style="color: #666666">.</span>get_attributes()
temp<span style="color: #666666">.</span>append(document<span style="color: #666666">.</span>get_scribd_url())
temp<span style="color: #666666">.</span>append(attrsDict[<span style="color: #4070a0">'thumbnail_url'</span>])
temp<span style="color: #666666">.</span>append(document<span style="color: #666666">.</span>get_download_url())
data<span style="color: #666666">.</span>append(temp)
extra_context[<span style="color: #4070a0">'data'</span>] <span style="color: #666666">=</span> data
<span style="color: #007020; font-weight: bold">return</span> render_to_response(<span style="color: #4070a0">'scribd.html'</span>, extra_context,
context_instance<span style="color: #666666">=</span>RequestContext(request))
</pre></div>
<br />
<b>In your scribd.html file............</b><br />
<br />
My Documents on scribd:<br />
<div class="highlight" ><pre><span style="color: #062873; font-weight: bold">< table</span> <span style="color: #4070a0">border='1'</span> <span style="color: #062873; font-weight: bold">></span>
<span style="color: #007020">{%</span> <span style="color: #007020; font-weight: bold">for</span> <span style="color: #bb60d5">d</span> <span style="color: #007020; font-weight: bold">in</span> <span style="color: #bb60d5">data</span> <span style="color: #007020">%}</span>
<span style="color: #062873; font-weight: bold">< tr></span>
<span style="color: #062873; font-weight: bold">< td</span> <span style="color: #4070a0">width='82px'</span><span style="color: #062873; font-weight: bold">><a</span> <span style="color: #4070a0">href='</span><span style="color: #007020">{{</span><span style="color: #bb60d5">d.0</span><span style="color: #007020">}}</span><span style="color: #4070a0">'</span>
<span style="color: #4070a0">style='color:blue;font-weight: normal;'</span><span style="color: #062873; font-weight: bold">></span>
Doc <span style="color: #007020">{{</span><span style="color: #bb60d5">forloop.counter</span><span style="color: #007020">}}</span>:<span style="color: #062873; font-weight: bold"></a></td></span>
<span style="color: #062873; font-weight: bold"><td><img</span> <span style="color: #4070a0">width=''</span> <span style="color: #4070a0">src='</span><span style="color: #007020">{{</span><span style="color: #bb60d5">d.1</span><span style="color: #007020">}}</span><span style="color: #4070a0">'</span>
<span style="color: #4070a0">alt='doc</span><span style="color: #007020">{{</span><span style="color: #bb60d5">forloop.counter</span><span style="color: #007020">}}</span><span style="color: #4070a0">'</span><span style="color: #062873; font-weight: bold">/></span>
<span style="color: #062873; font-weight: bold"></td></span>
<span style="color: #062873; font-weight: bold"><td><a</span> <span style="color: #4070a0">href='</span><span style="color: #007020">{{</span><span style="color: #bb60d5">d.2</span><span style="color: #007020">}}</span><span style="color: #4070a0">'</span> <span style="color: #4070a0">style='color:blue;</span>
<span style="color: #4070a0">font-weight: normal;'</span><span style="color: #062873; font-weight: bold">></span>
Download doc <span style="color: #007020">{{</span><span style="color: #bb60d5">forloop.counter</span><span style="color: #007020">}}</span>
<span style="color: #062873; font-weight: bold"></a></td></span>
<span style="color: #062873; font-weight: bold"></tr></span>
<span style="color: #007020">{%</span> <span style="color: #007020; font-weight: bold">endfor</span> <span style="color: #007020">%}</span>
<span style="color: #062873; font-weight: bold"></table></span>
</pre></div>
<br />
<span style="font-size: large;"><span style="background-color: #6fa8dc;">Live Demo</span></span><br />
<span style="background-color: black; color: white;"></span><a href="http://coderpriyu.alwaysdata.net/scribd/" style="color: blue;" target="_blank">http://coderpriyu.alwaysdata.net/scribd/</a><br />
<br />
<br />
<b>Thats it....................</b></div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-35846259256405555612011-12-06T03:03:00.001-08:002012-06-06T22:51:08.544-07:00Database Connection Pool Solution for Django + Mysql<div dir="ltr" style="text-align: left;" trbidi="on">
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
<div class="entry">
<strong>The solution:</strong><br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
An alternative DATABASE_ENGINE for django that leverages core django mysql code with minimal overlap and sqlalchemy for connection pooling. Also, a pretty ‘DRY’ stub to mix in your own pooling if desired, or adapt to use postgres instead of mysql.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
1. Install sqlalchemy library: <a href="http://www.sqlalchemy.org/download.html">http://www.sqlalchemy.org/download.html</a><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> I used “Latest 0.5? myself.<span class="Apple-style-span" style="white-space: pre;"> </span><br />
2. Grab: <a href="http://node.to/wordpress/wp-content/uploads/2008/09/mysql_pool.tgz">mysql_pool.tgz</a><br />
<br />
3. Make mysql_pool reachable by your project<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>3a. Unpack in your python’s “site-packages” directory<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>3b. Or: unpack somewhere in your project directory, and edit the “base.py” file in mysql_pool to fix the import lines containing uw.udjango.db.engine to be the new location mysql_pool.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
4. Edit your settings.py to change DATABASE_ENGINE<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>4a. If 3a, set to uw.udjango.db.engine.mysql_pool<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>4b. If 3b, set to yourproject.whatever.mysql_pool<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
5. Edit your settings.py to add these (required) tuning settings:<br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
<span class="Apple-tab-span" style="white-space: pre;"> </span>DBPOOL_WAIT_TIMEOUT = 28800 # your mysql db’s server side inactive connection kill time<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span># discernable by ’show GLOBAL variables;’ in mysql, look for ‘wait_timeout’, changeable if desired<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>DBPOOL_SIZE = 20 # the maintained number of dbconnections, over this returned conns are destroyed<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>DBPOOL_MAX = 100 # the max allow connections, period<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>DBPOOL_INTERNAL_CONN_TIMEOUT = 10 # how long to wait for mysql to give you a connection<br />
</pre>
</div>
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
That should do it!<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
<strong>The explanation:</strong><br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
I’ve been reading up on the various was being proposed to make some kind of persistent connection re-use part of core django. The are a couple of options I like, especially having a core database engine choice of sqlalchemy. A lot of implications there though, and I didn’t want to tackle them all.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
At first I figured I’d write my own, and stubbed out the code to do that. Since I’m using mysql, I copied it’s engine tree in django.db.backends into my project and began working on it. Quickly I realized I didn’t want to rewrite, or maintain duplicates of, the whole tree. I only cared about two calls really: self.connection = Database.connect() and self.connection.close(). Modifying those would be a way to plug in pooling of my own. Also, in the process I looked to SQLAlchemy’s pool code as an example of Python implementation, and it seemed pretty good. Eventually I’d like to add some more subtlety to the pool grow/shrink strategy.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
I started riffing on the solution described on <a href="http://menendez.com/blog/mysql-connection-pooling-django-and-sqlalchemy/">Ed Menendez’s site</a>, and merging in some of my ideas to make it more DRY.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
Basically, all the classes in mysql_pool use class naming tricks to extend-without-modification and assume the identity and characteristics of the core mysql engine. That means there is no code in mysql_pool to maintain other than the “base.py” file, and even that one is able to use the same short cut for most of its classes, the exception being DatabaseWrapper, which is updated to use SQLAlchemy’s pool.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
Also, I added settings.py level control of SQLAlchemy’s QueuePool, which is the pool type mysql_pool is forced to use.<br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
Other links of note on related topics: <br />
<span class="Apple-style-span" style="white-space: pre;"> </span><br />
<a href="http://groups.google.com/group/django-developers/browse_thread/thread/9f0353fe0682b73">Proposal: user-friendly API for multi-database support</a><br />
<br /></div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-2863445126208394102011-12-05T22:29:00.001-08:002011-12-07T20:55:22.029-08:00Django: How to add Google +1 button to your website.<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="post hentry">
<a href="http://www.blogger.com/blogger.g?blogID=8511780156263103084" name="5741961449215739569"></a><br />
<div class="post-body entry-content" id="post-body-5741961449215739569">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA3hZIkfnz5CyNnXQ0QhnwDaoKv54do5GyDMqS_Ty4-bf_mAo7SoWhyphenhyphennAOYDqEpuU70R8CfV6jWxeGl9WPHAOYY__fPwVA3kdxY8k1sAEAt214ihyphenhyphen7CeMiWVSA-F57z-ffcdqkSdihKAEi/s1600/google-1-button.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA3hZIkfnz5CyNnXQ0QhnwDaoKv54do5GyDMqS_Ty4-bf_mAo7SoWhyphenhyphennAOYDqEpuU70R8CfV6jWxeGl9WPHAOYY__fPwVA3kdxY8k1sAEAt214ihyphenhyphen7CeMiWVSA-F57z-ffcdqkSdihKAEi/s200/google-1-button.jpg" width="200" /></a></div>
<h2>
What is Google +1 button?</h2>
<div style="font-family: Times,"Times New Roman",serif;">
It's new Google social button. It's much similar to Facebook "Like" button. While available during one month it earned popularity that could compete with Facebook's social button. Corporation of Good knows what to do, so you probably want to have one on your website inline with Facebook's Like...</div>
<h2>
Add Google +1 button to your site:</h2>
<h2>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;">- Open <a href="http://www.google.com/webmasters/+1/button/index.html">http://www.google.com/webmasters/+1/button/index.html</a> and generate own button for your website.</span></span></h2>
<div>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;">Google proposes HTML code like this:</span></span></div>
<div>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-family: Helvetica, Arial, sans-serif; font-size: xx-small;"></span></span></span><br />
<pre class="prettyprint" style="background-color: #fafafa; border-bottom-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(187, 187, 187); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(187, 187, 187); border-top-style: solid; border-top-width: 1px; color: #007000; font-family: monospace; font-size: 9pt; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 1em; overflow-x: auto; overflow-y: auto; padding-bottom: 0.99em; padding-left: 0.99em; padding-right: 0.99em; padding-top: 0.99em; word-wrap: break-word;"><span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-family: Helvetica, Arial, sans-serif; font-size: xx-small;"><span class="tag" style="color: #000088;"><html></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"><head></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"><title></span><span class="pln" style="color: black;">+1 demo: Basic page</span><span class="tag" style="color: #000088;"></title></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"><link</span><span class="pln" style="color: black;"> </span><span class="atn" style="color: #660066;">rel</span><span class="pun" style="color: #666600;">=</span><span class="atv" style="color: #008800;">"canonical"</span><span class="pln" style="color: black;"> </span><span class="atn" style="color: #660066;">href</span><span class="pun" style="color: #666600;">=</span><span class="atv" style="color: #008800;">"http://www.example.com"</span><span class="pln" style="color: black;"> </span><span class="tag" style="color: #000088;">/></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"><script</span><span class="pln" style="color: black;"> </span><span class="atn" style="color: #660066;">type</span><span class="pun" style="color: #666600;">=</span><span class="atv" style="color: #008800;">"text/javascript"</span><span class="pln" style="color: black;"> </span><span class="atn" style="color: #660066;">src</span><span class="pun" style="color: #666600;">=</span><span class="atv" style="color: #008800;">"https://apis.google.com/js/plusone.js"</span><span class="tag" style="color: #000088;">></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"></script></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"></head></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"><body></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"><g:plusone></g:plusone></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"></body></span><span class="pln" style="color: black;">
</span><span class="tag" style="color: #000088;"></html></span></span></span></span></pre>
</div>
<div>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">It's quite simple, as you can see. And that's much pretty it. Oh no wait...</span></div>
<div>
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">- Add some variable to your template like "google_target_url" like this:</span></div>
<div>
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px;"></span></span><br />
<br />
<pre style="background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px; max-height: 600px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; vertical-align: baseline; width: auto;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px;"><code style="background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">#pass variable to template with google +1 target url
return render_to_response("template.html",
{"google_target_url":google_target_url,})</code></span></span></pre>
</div>
<div>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<span style="font-size: small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;">Finally you can add property "href" (similar to common </span><span class="Apple-style-span" style="color: #444444; font-family: 'Trebuchet MS', sans-serif;"><i>a href=" "</i></span><span style="font-family: Times, 'Times New Roman', serif;"> pattern) to target +1 action to custom url. In general you can now generate page with multiple +1 buttons on it. For e.g.: one button under one blogpost or image...</span></span></div>
<div>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-family: Helvetica, Arial, sans-serif; font-size: xx-small;"></span></span></span><br />
<pre class="prettyprint" style="background-color: #fafafa; border-bottom-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(187, 187, 187); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(187, 187, 187); border-top-style: solid; border-top-width: 1px; font-family: monospace; font-size: 9pt; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 1em; overflow-x: auto; overflow-y: auto; padding-bottom: 0.99em; padding-left: 0.99em; padding-right: 0.99em; padding-top: 0.99em; word-wrap: break-word;"><span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-family: Helvetica, Arial, sans-serif; font-size: xx-small;"><span class="tag" style="color: #000088;"><g:plusone href="</span><span class="tag"><span class="Apple-style-span" style="color: #38761d;">http://www.example.com/custom_page1/</span></span><span class="tag" style="color: #000088;">" size="</span><span class="tag"><span class="Apple-style-span" style="color: #38761d;">standard</span></span><span class="tag" style="color: #000088;">" count="</span><span class="tag"><span class="Apple-style-span" style="color: #38761d;">false</span></span><span class="tag" style="color: #000088;">"></g:plusone></span></span></span></span></pre>
</div>
<div>
<span style="font-size: x-small; font-weight: normal;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<h2>
</h2>
<div style="clear: both;">
</div>
</div>
</div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-90150001105713085262011-12-05T22:07:00.000-08:002011-12-05T22:24:19.066-08:00Django - compressing CSS/JS files with django-compressor<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib1DrUuhdfAucb0McW_l3tUgk8hbYMRVNOIM4oM9F_Q1B8qkhNgtqM55jZii3XShd-FcL_t_h-s0cz4NaroHJalK3YN_iolkBE-MTMtyLeTKKM26VtftpfTclGuKgpHBCoEq_6I7ZWBSX1/s1600/pump_air_compressor.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="292" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib1DrUuhdfAucb0McW_l3tUgk8hbYMRVNOIM4oM9F_Q1B8qkhNgtqM55jZii3XShd-FcL_t_h-s0cz4NaroHJalK3YN_iolkBE-MTMtyLeTKKM26VtftpfTclGuKgpHBCoEq_6I7ZWBSX1/s320/pump_air_compressor.jpg" width="320" /></a>There are 2 main usual tasks with web project's deployment and about .css and .js files.<br />
<br />
First one - size minimization. But there are lot's of utilities helping you to compress CSS files. Delete unused spaces, comments and so on... Second one - Version control. For e. g. When you've updated the script on your deployment server, but user's browser uses old one until user manually hits 'Refresh'.<br />
<br />
That's where Static files compressor comes in to mind.<br />
<br />
<br />
<br />
Upon selecting among available one's found top "google" results:<br />
- <a href="https://github.com/pelme/django-compress">django-compress</a><br />
- <a href="https://github.com/mintchaos/django_compressor">django-compressor</a><br />
- <a href="http://elsdoerfer.name/docs/webassets/">webassets</a><br />
<br />
Project uses 'django.contrib.staticfiles', so django-compress was not compatible... It does not support Django's static files gently. Webassets s a good lib. but project has a huge amount of different static. Maybe it's ok for e small project, but when you need to specify/change a 100's javascript in python module for certain templates... Nothing good comes in mind. And imho TEMPLATES... That's where thing's like those should live.<br />
<br />
So <a href="https://github.com/mintchaos/django_compressor">Django-compressor</a> was chosen using those criteria. And, to mention, <a href="https://github.com/mintchaos/django_compressor">project</a> is actively developed, supported and <a href="http://django_compressor.readthedocs.org/">documented</a>. Anyway adding it to your django project is quite simple, as most good apps.<br />
<br />
What it does, it turns this:<br />
<br />
<pre style="background-color: black; color: #55cc66;"><span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script type="text/javascript" src="{{ MEDIA_URL }}public/js
/jquery-1.4.1.min.js"</span><span style="color: #865700;">></span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">script</span><span style="color: #865700;">></span>
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script type="text/javascript" src="{{ MEDIA_URL }}banners/js/</span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #508050; font-weight: bold;">jquery.cycle.all.js"</span><span style="color: #865700;">></span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">script</span><span style="color: #865700;">></span>
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script type="text/javascript" src="{{ MEDIA_URL }}js/gb/greybox.js"</span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #865700;">></span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">script</span><span style="color: #865700;">></span>
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script type="text/javascript"</span><span style="color: #865700;">></span>jQuery<span style="color: #808030;">.</span>browser<span style="color: #808030;">.</span>msie6 <span style="color: #808030;">=</span> <span style="color: #508050; font-weight: bold;">
</span>jQuery<span style="color: #808030;">.</span>browser<span style="color: #808030;">.</span>msie <span style="color: #808030;">&&</span> <span style="color: #508050; font-weight: bold;">parseInt</span><span style="color: #808030;">(</span>jQuery<span style="color: #808030;">.</span>browser<span style="color: #808030;">.</span>version<span style="color: #808030;">)</span> <span style="color: #508050; font-weight: bold;">
</span><span style="color: #808030;">==</span> <span style="color: #778c77;">6</span> <span style="color: #808030;">&&</span> <span style="color: #808030;"> </span></pre>
<pre style="background-color: black; color: #55cc66;"><span style="color: #808030;">!</span>window<span style="color: #808030;">[</span><span style="color: maroon;">"</span><span style="color: #cc5555;">XMLHttpRequest</span><span style="color: maroon;">"</span><span style="color: #808030;">]</span><span style="color: purple;">;</span>
<span style="color: #508050; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #808030;">!</span>jQuery<span style="color: #808030;">.</span>browser<span style="color: #808030;">.</span>msie6<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
$<span style="color: #808030;">(</span>document<span style="color: #808030;">)</span><span style="color: #808030;">.</span>ready<span style="color: #808030;">(</span><span style="color: #508050; font-weight: bold;">function</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">{</span>
$<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #cc5555;">div#body_wrapper</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span>wrap<span style="color: #808030;">(</span><span style="color: #cc5555;">'<div id="body_wrapper_shadow_right"</span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #cc5555;">><div id="body_wrapper_shadow_left">'</span><span style="color: #808030;">+</span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #cc5555;">'</div></div>'</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span><span style="color: #808030;">)</span><span style="color: purple;">;</span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #808030;"><</span><span style="color: #808030;">/</span>script<span style="color: #808030;">></span></pre>
<br />
Into this:<br />
<br />
<pre style="background-color: #001800; color: #55cc66;"><span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script type="text/javascript" src="/static/CACHE/js/8dd1a2872443.js" </span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #508050; font-weight: bold;">charset="utf-8"</span><span style="color: #865700;">></span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">script</span><span style="color: #865700;">></span></pre>
<br />
The install is quite simple and you need to <a href="http://django_compressor.readthedocs.org/en/latest/installation/">follow this manual</a>.<br />
Then configure it. Basically you hava all set for it to function. Except for me having a bit wired way of storing static files. I had to remap default <span class="Apple-style-span" style="color: #666666;">COMPRESS_ROOT</span> and <span class="Apple-style-span" style="color: #666666;">COMPRESS_URL</span> variables. It's quite easily done in <span class="Apple-style-span" style="color: #666666;">settings.py</span>. And it has lot's of other <a href="http://django_compressor.readthedocs.org/en/latest/settings/">settings</a>, that I did not require.<br />
<br />
Anyhow this is it. Now you can wrap all of your code with handy templatetags that will do all the work for you. it may look somehow like this:<br />
<br />
<pre style="background-color: #001800; color: #55cc66;">{% load compress %}
{% compress css %}
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">link</span><span style="color: #477766;"> </span><span style="color: #477766;">rel</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"stylesheet"</span><span style="color: #477766;"> </span><span style="color: #477766;">href</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"/static/css/one.css"</span><span style="color: #477766;"> </span><span style="color: #477766;">type</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"text/css"</span><span style="color: #477766;"> </span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #477766;">charset</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"utf-8"</span><span style="color: #865700;">></span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">style</span><span style="color: #865700;"> type="text/css"></span><span style="color: #508050; font-weight: bold;">p</span> <span style="color: purple;">{</span> <span style="color: #bb7977; font-weight: bold;">border</span><span style="color: #808030;">:</span><span style="color: #778c77;">5</span><span style="color: #778c77;">px</span> <span style="color: #477766;">solid</span> green<span style="color: purple;">;</span><span style="color: purple;">}</span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">style</span><span style="color: #865700;">></span>
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">link</span><span style="color: #477766;"> </span><span style="color: #477766;">rel</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"stylesheet"</span><span style="color: #477766;"> </span><span style="color: #477766;">href</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"/static/css/two.css"</span><span style="color: #477766;"> </span><span style="color: #477766;">type</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"text/css"</span><span style="color: #477766;"> </span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #477766;">charset</span><span style="color: #808030;">=</span><span style="color: #cc5555;">"utf-8"</span><span style="color: #865700;">></span>
{% endcompress %}</pre>
<br />
or for .js files:<br />
<br />
<pre style="background-color: #001800; color: #55cc66;">{% load compress %}
{% compress js %}
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script src="/static/js/one.js" type="text/javascript" charset="utf-8"</span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #865700;">></span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">script</span><span style="color: #865700;">></span>
<span style="color: #865700;"><</span><span style="color: #508050; font-weight: bold;">script type="text/javascript" charset="utf-8"</span><span style="color: #865700;">></span>obj<span style="color: #808030;">.</span>value <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #cc5555;">value</span><span style="color: maroon;">"</span><span style="color: purple;">;</span><span style="color: #865700;"> </span><span style="color: #508050; font-weight: bold;">
</span><span style="color: #865700;"></</span><span style="color: #508050; font-weight: bold;">script</span><span style="color: #865700;">></span>
{% endcompress %}</pre>
<br />
It will generate a custom .js/.css files with all of your compressed static at one file in your media/static dir that can be cashed. They will have links, like <b style="color: #666666;">"/media/CACHE/css/105dsb963311.css"</b>and put your newly generated script/style there.<span class="Apple-style-span" style="background-color: white; font-family: trebuchet,Verdana,Geneva; font-size: 13px; line-height: 18px;"> </span><br />
<br />
This is it. Use this in your projects. It economies your server's time/traffic and helps you to update scripts at all user's clients in their browsers when you deploy a new version.<br />
</div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-33591033897245810002011-12-03T01:36:00.001-08:002011-12-03T01:43:08.379-08:00Django Tips & Features before getting Started<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
1. Dont hardcode MEDIA_ROOT and TEMPLATE_DIRS use </div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
os.path.realpath(os.path.dirname)__file__)) </div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span>hardcoding will cause problem while moving from dev->test->live</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
2. Dont hardcode static files in templates. use MEDIA_URL</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span>EG: </div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span><script src="{{ MEDIA_URL }}js/jquery.js" type="text/javascript">
</script></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span>In future if you change your cdn, you will have to change it only in the settings</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span>To get the context variable use django context processor. Its very easy to write and this makes MEDIA_URL and any other variable you like available across all the templates </div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors" target="_blank"><span class="Apple-style-span" style="color: red; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;">http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors</span></a></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
3. Use the url function to define the urls. Dont hardcode urls. Use reverse() in views.py and {% url %} tag in templates</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
4. Thirdparty app django-command-extension</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span>This app is very useful. It gives some other useful commands like</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
-> shell_plus - extension of shell. It will autoload all enabled django models.</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 60px; padding-right: 0px; padding-top: 0px;">
-> sqldiff - gives you the sql diff between the code and the DB</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
-> show_urls - displays the url routes that are defined in the project</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
-> graph_models - creates a GraphViz dot file </div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
-> clean_pyc - removes all the pyc files (Eclipse helois pydev makes it very easy...)</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
-> reset_db - Resets a database</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
5. use pdb to debug django projects</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
6. dont copy paste html across templates. use {% extends %}, {% include %} and other builtin templatetags. write your own custom templatetags if necessary (they are very easy to write)</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<a href="http://docs.djangoproject.com/en/1.1/topics/http/middleware/" target="_blank">http://docs.djangoproject.com/en/dev/ref/templates/builtins/</a></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
7. understand django middleware. Use this if you want to do some something before the request is processed / after the response is generated etc. - again they are very easy to write</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<a href="http://docs.djangoproject.com/en/1.1/topics/http/middleware/"><span style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></span>http://docs.djangoproject.com/en/1.1/topics/http/middleware/</a></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 30px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
8. use django forms - they are really really powerful </div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
9. use django test client for unit testing. It acts a dummy web browser. Its very useful to simulate GET and POST request</div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<a href="http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client">http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client</a></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="background-color: rgb(255, 255, 255) !important; border-bottom-color: rgb(255, 255, 255) !important; border-left-color: rgb(255, 255, 255) !important; border-right-color: rgb(255, 255, 255) !important; border-top-color: rgb(255, 255, 255) !important; font-family: 'Lucida Grande', Arial, Geneva, sans-serif; font-size: 14px; line-height: 1.4em; margin-bottom: 5px; margin-top: 5px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-39086891263073088512011-12-03T01:30:00.001-08:002012-06-06T22:56:21.770-07:00Hello world in Ruby on Rails step by step<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<dd>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I just started to learn ROR just because it is quite similar to Django</span><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"><br /></span><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">So, this is a step by step tutorial to getting over the first
hurdle: Creating a “hello world” application in Ruby on Rails. This
should work assuming you already have Rails 3 installed and running on
your system:</span><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;margin-left:-38px;">
<ol>
<li>
<pre><code class=""shell""><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">$ rails hello</span></code></pre>
</li>
<li>
<pre><code class=""shell""><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">$ cd hello</span></code></pre>
</li>
<li>
<pre><code class=""shell""><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">$ rails generate controller hello</span></code></pre>
</li>
<li>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Open the file config/routes.rb. Almost at the bottom (line #57) is this line:</span><br />
<pre><code class=""ruby""><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"># match ':controller(/:action(/:id(.:format)))'</span></code></pre>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Remove the # in front so the line looks like:</span><br />
<br />
<pre><code class=""ruby""><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">match ':controller(/:action(/:id(.:format)))'</span></code></pre>
</li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Create a file named index.html.erb in app/views/hello containing the text ‘Hello world’.</span></li>
<li>
<pre><code class=""shell""><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">$ rails server</span></code></pre>
</li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Navigate to <a href="http://localhost:3000/hello">http://localhost:3000/hello</a> in your browser and be greeted by your friendly application: “Hello world”.</span></li>
</ol>
</div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">PS: <a href="http://localhost:3000/">http://localhost:3000/</a> is (still) a lot prettier “Hello world”-ish page.</span><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"><span class="caps"><span class="caps">PPS</span></span>: Note that
the Rails 3 example above actually has a step more than the Rails 1
ditto. This comes from the fact that Rails 3 doesn’t build a default
route for you.</span><br />
<div class="aside versions">
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">
The code in this article has been verified with:<br />
</span><ul>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Ruby: 1.8.7</span></li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Rails: 3.0.0.beta</span></li>
</ul>
</div>
</dd></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-60410454314177123632011-12-03T01:22:00.001-08:002012-06-06T23:01:37.500-07:00Ruby on Rails on windows with mysql<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="post-text">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7Ezqbg8vXAYyVArkP_Gk2VxZyQC9FX8xi_g3Ci6oYTK5RiIi5ZuIUJpiY03GpfqpE2yMYQ8VeoPxQGfU-cMkJ4yfTpk_uGLqWDP-GPMskl9vT96eo8LE-rsQDd8hSpX-odsylM9YTW40/s1600/ruby_on_rails_logo.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7Ezqbg8vXAYyVArkP_Gk2VxZyQC9FX8xi_g3Ci6oYTK5RiIi5ZuIUJpiY03GpfqpE2yMYQ8VeoPxQGfU-cMkJ4yfTpk_uGLqWDP-GPMskl9vT96eo8LE-rsQDd8hSpX-odsylM9YTW40/s320/ruby_on_rails_logo.jpg" width="268" /></a><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"> </span></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"> This is basically what I do to install on Windows XP:</span><br />
<ul>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Download and install latest mysql from <a href="http://dev.mysql.com/downloads/mysql/" rel="nofollow">http://dev.mysql.com/downloads/mysql/</a></span></li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Install Ruby <a href="http://rubyinstaller.org/downloads/" rel="nofollow">http://rubyinstaller.org/downloads/</a> (check the box to update PATH)</span></li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Install Ruby Devkit <a href="http://rubyinstaller.org/downloads/" rel="nofollow">http://rubyinstaller.org/downloads/</a> (for example extract to C:\DevKit)</span></li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Setup Devkit</span><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;margin-left:-38px;">
<pre>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">cd [MY_DEVKIT_INSTALL_DIR]</span>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">ruby dk.rb init</span>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">ruby dk.rb install</span>
</pre>
</div>
</li>
<li><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Copy libmysql.dll from your mysql/bin install to C:\Ruby192\bin</span><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;margin-left:-38px;">
<pre>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">gem install rails</span>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">gem install mysql2 -v 0.2.6</span>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">rails new my_test_app -d mysql</span>
</pre>
</div>
</li>
</ul>
</div>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-76524736917185784402011-11-16T23:57:00.000-08:002012-06-06T23:17:59.609-07:00Django - Google maps on your page and display address in map entered in form<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOvnHpQlA7CdX5ID0X-1BHyNDTNJAuzaNhjdGCW_0KWUDoH7gRIpLfFUpn-eLRRAgfdlW6Qv2OO_2yiuOGXIPEFG5kzy6AlulKfX4ND7KQWSrH7KNa2slux0jm68_NES-yX9VAKiuO7M4/s1600/snap_gmap.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOvnHpQlA7CdX5ID0X-1BHyNDTNJAuzaNhjdGCW_0KWUDoH7gRIpLfFUpn-eLRRAgfdlW6Qv2OO_2yiuOGXIPEFG5kzy6AlulKfX4ND7KQWSrH7KNa2slux0jm68_NES-yX9VAKiuO7M4/s400/snap_gmap.JPG" width="400" /></a></div>
<i><br /></i><br />
<i>1)</i> get gmapi , Django application for google map , from here http://code.google.com/p/django-gmapi/<br />
<br />
<i>2) copy gmapi folder to your project directory </i><br />
<br />
<i>3) add gmapi to INSTALLED_APP and in urls.py add following two lines.</i><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
url(r'', include('gmapi.urls.media')), # Use for debugging only.
url(r'^$', 'main.views.index'),
</pre>
</div>
<br />
<i>4) in your main/views.py</i><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
def index(request):
"intialize gmap with initial center location"
gmap = maps.Map(opts = {
'center': maps.LatLng(18, 72.5),
'mapTypeId': maps.MapTypeId.ROADMAP,
'zoom': 3,
'mapTypeControlOptions': {
'style': maps.MapTypeControlStyle.DROPDOWN_MENU
},
})
""" This is my location """
xyam = 23 # longitude
yyam = 72.5 # latitude
""" for display marker on my location """
marker = maps.Marker(opts = {
'map': gmap,
'position': maps.LatLng(xyam, yyam),
})
maps.event.addListener(marker, 'mouseover', 'myobj.markerOver')
maps.event.addListener(marker, 'mouseout', 'myobj.markerOut')
info = maps.InfoWindow({
'content': "Priyank's location",
'disableAutoPan': True
})
info.open(gmap, marker)
""" to get address and display result on maps """
if request.method == 'POST':
from gmapi.maps import Geocoder
geocoder = Geocoder()
address = request.POST.get('address')
results, status_code = geocoder.geocode({'address': address })
if results:
for r in results:
result = r
#print "location====",result['geometry']['location']
lat, lng = result['geometry']['location']['arg']
xyam = lat
yyam = lng
""" This markers are for search results """
marker1 = maps.Marker(opts = {
'map': gmap,
'position': maps.LatLng(xyam, yyam),
})
maps.event.addListener(marker1, 'mouseover', 'myobj.markerOver')
maps.event.addListener(marker1, 'mouseout', 'myobj.markerOut')
info = maps.InfoWindow({
'content': "Searched result",
'disableAutoPan': True
})
info.open(gmap, marker1)
extra_context = {}
extra_context['form'] = MapForm(initial={'map': gmap})
return render_to_response('index.html', extra_context, context_instance=RequestContext(request))
</pre>
</div>
<i>5) in your index.html put this line....</i><br />
<div class="codehilite" style="display: block; overflow: hidden; width: 565px;">
<pre>
{{ form.map }}
{{ form.media.js }}
<span class="Apple-tab-span" style="white-space: pre;"> </span>< script type="text/javascript" >
window.myobj = {
markerOver: function() { this.openInfoWindow(); },
markerOut: function() { this.closeInfoWindow(); }
};
</script>
<form action='' method='post'>
{% csrf_token %}
Address:<input type='text' name='address'/>
<input type='submit' value='Get location'/>
</form>
</pre>
</div>
<i>6)<span class="Apple-tab-span" style="white-space: pre;"> </span>here is live example...</i>....<a href="http://coderpriyu.alwaysdata.net/">http://coderpriyu.alwaysdata.net</a><br />
<br />
<i>7) </i>thats it . stay tune with blog , i w'll post shortly about how to display your live location in your page with google latitude api..<br />
<br />
<i>8) </i>question ??.............comment it ...<br />
<div>
<br /></div>
</div>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-8511780156263103084.post-61784274417686304162011-11-04T00:26:00.000-07:002011-11-04T00:26:30.312-07:00Guide to Create Web service using JAVA and Apache Axis2 using Eclipse With Screen shot<h5>Web services are application components which communicate using open protocols. Using Web Services we can publish our application's functions to everyone. This tutorial provides step by step instructions to develop Web Services using Axis2 Web Services / SOAP / WSDL engine and Eclipse IDE. Let's start.</b>
</h5>
<h3><b>Here is the step by step guide...........<a href="http://blog.sencide.com/2011/06/create-web-service-using-apache-axis2.html">Click here</a></b></h3>
<br/>
<br/>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-6005798974967456302011-10-25T00:16:00.000-07:002011-10-25T00:17:00.223-07:00Django with solr - Now you are true web-dev<div id="mainContent">
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/solr.jpg" alt="" title="solr" width="380" height="283" style="border:1px solid;" class="alignnone size-full wp-image-1454"><br>
</center><p></p>
<p><i><a href="http://www.curiosity-media.com/blog/use-apache-solr-for-lighting-fast-database-1395.php">Apache Solr</a></i> is an extremely powerful, enterprise level search engine, and can be used to store billions of records. For anyone with experience in MySql, you will understand how query time starts to degrade after reaching around 1,000,000 rows for any given table. After doing tons of research to try to find an alternative method for a quick and reliable search database, I stumbled upon the <i>Apache Solr</i> Project. The general consensus about <i>Apache Solr</i> is that it’s lightning fast, and after using it for a recent project I will definitely agree to this.</p>
<p><span id="more-1439"></span></p>
<p>So this should be great news for a web developer who is looking for such a solution. Just go to the <i>Apache Solr</i> website, download and install the software, and you’re set right? Wrong! To give you a fair warning, the integration of <i>Apache Solr</i> onto your web server is a complete project in itself. The reason it’s so difficult is because of the lack of quality information online, so I’d like to share my knowledge to all you <i>Apache Solr</i> Noobs so you don’t have to rip your hair out your skull. If I can save just one hair follicle from this guide, then I’ve done my job. For women with mustaches, you may not want to continue reading as you may want to lose some facial hair.</p>
<p><br><br><br>
<strong>The Downloads</strong></p>
<p>Before I get started, I should let everyone know that this guide is mainly for Windows XP users, although there is a slight variation to the steps for anyone using the new Windows 7.</p>
<p>1. Download Xampp For Windows, Basic Package. <a href="http://www.apachefriends.org/en/xampp-windows.html" target="_blank">http://www.apachefriends.org/en/xampp-windows.html</a></p>
<p>2. Download Tomcat Add-on. Tomcat is a java server and because Solr is run on Java getting Tomcat is necessary.</p>
<p>3. Download Java JDK <a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">http://java.sun.com/javase/downloads/index.jsp</a></p>
<p>4. Download <i>Apache Solr</i> from one of the mirrors. I got version 1.4.0 but I believe any version will do. <a href="http://www.proxytracker.com/apache/lucene/solr/" target="_blank">http://www.proxytracker.com/apache/lucene/solr/</a></p>
<p>5. Download the Solr PHP Client. <a href="http://code.google.com/p/solr-php-client/" target="_blank">http://code.google.com/p/solr-php-client/</a></p>
<p><br><br><br>
<strong>The Installation</strong></p>
<p>1. Install Xampp, and follow the instructions.</p>
<p>2. Install Tomcat, and follow the instructions.</p>
<p>3. Install the latest java JDk.</p>
<p>4. There should now be a folder called /xampp in your C Drive. Enter the xampp folder and find the ‘xampp-control’ application, and start it. </p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/011.jpg" alt="" title="011" width="500" height="241" style="border:1px solid;" class="alignnone size-full wp-image-1442"><br>
</center><p></p>
<p>5. Place a check mark for the Svc for Apache, MySQL, and Tomcat. This is so you install these applications as windows services.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/021.jpg" alt="" title="021" width="446" height="382" class="alignnone size-full wp-image-1443"><br>
</center><p></p>
<p>6. Click the ‘SCM’ button and you should get a Windows Service Window.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/03.jpg" alt="" title="03" width="446" height="382" class="alignnone size-full wp-image-1444"><br>
</center><p></p>
<p>7. Find the Apache Tomcat Service, then Right click it and go to ‘Properties’. Here you will set the Startup Type to Automatic, and close the properties window. We want Tomcat to start every time Windows boots up.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/041.jpg" alt="" title="041" width="500" height="352" class="alignnone size-full wp-image-1445"><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/05.jpg" alt="" title="05" width="410" height="468" class="alignnone size-full wp-image-1446"><br>
</center><p></p>
<p>8. Now highlight Apache Tomcat in the Services Window, and click the option to Stop the Service if it’s not already Stopped. Tomcat has to be disabled for the next few steps.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/06.jpg" alt="" title="06" width="500" height="354" class="alignnone size-full wp-image-1447"><br>
</center><p></p>
<p>9. Extract <i>Apache Solr</i>, then go into the /dist folder. There should be a file called apache-solr-1.4.0.war, copy this file.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/071.jpg" alt="" title="071" width="380" height="151" style="border:1px solid;" class="alignnone size-full wp-image-1448"><br>
</center><p></p>
<p>10. Now find a folder in C:/xampp/tomcat/webapps/ and copy the apache-solr-1.4.0.war file into this folder. Rename apache-solr-1.4.0.war to solr.war.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/08.jpg" alt="" title="08" width="500" height="173" style="border:1px solid;" class="alignnone size-full wp-image-1449"><br>
</center><p></p>
<p>11. Go back to the extracted Apache Solr folder and go to /example/solr/ then copy these files.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/09.jpg" alt="" title="09" width="413" height="164" style="border:1px solid;" class="alignnone size-full wp-image-1450"><br>
</center><p></p>
<p>12. Create a New directory in C:/xampp/ called /solr/. You will now paste the /example/solr/ files into this directory.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/101.jpg" alt="" title="101" width="500" height="92" style="border:1px solid;" class="alignnone size-full wp-image-1451"><br>
</center><p></p>
<p>13. Now find C:/xampp/tomcat/bin/tomcat6w, click on the Java Tab, and copy the command “-Dsolr.solr.home=C:xamppsolr” into the Java Options section.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/11.jpg" alt="" title="11" width="416" height="403" class="alignnone size-full wp-image-1452"><br>
</center><p></p>
<p>14. Now go back to the Windows Services Window, and start Apache Tomcat.</p>
<p>15. Open up a browser and type “http://localhost:8080/solr/admin/” into the browser to confirm a successful installation of <i>Apache Solr</i>. You should see the <i>Apache Solr</i> Administrative Screen, if you see a bunch of error codes then you messed up. You might want to consider uninstalling everything, then start over and follow directions more carefully next time.</p>
<p></p><center><br>
<img src="http://www.curiosity-media.com/blog/wp-content/uploads/2010/04/12.jpg" alt="" title="12" width="500" height="313" class="alignnone size-full wp-image-1453"><br>
</center><p></p>
<p><br><br><br>
</div>
<!-- -------------------------------------- Solr Python library----------------------------------------- -->
<h1>Python libraries to access Solr</h1>
<div dir="ltr" id="content" lang="en"><span class="anchor" id="top"></span>
<span class="anchor" id="line-1"></span><p class="line867"></p><div class="table-of-contents"><p class="table-of-contents-heading">Contents</p><ol><li>
<a href="#Python_API">Python API</a></li><li>
<a href="#solrpy">solrpy</a></li><li>
<a href="#PySolr">PySolr</a></li><li>
<a href="#insol">insol</a></li><li>
<a href="#sunburnt">sunburnt</a></li><li>
<a href="#Using_Solr.27s_Python_output">Using Solr's Python output</a></li><li>
<a href="#Using_normal_JSON">Using normal JSON</a></li></ol></div> <span class="anchor" id="line-2"></span><span class="anchor" id="line-3"></span><p class="line867">
</p><h2 id="Python_API">Python API</h2>
<span class="anchor" id="line-4"></span><p class="line862">There is a simple client API as part of the Solr repository: <a class="http" href="http://svn.apache.org/viewvc/lucene/solr/tags/release-1.2.0/client/python/">http://svn.apache.org/viewvc/lucene/solr/tags/release-1.2.0/client/python/</a> <span class="anchor" id="line-5"></span><span class="anchor" id="line-6"></span></p><p class="line874">Note: As of version 1.3, Solr no longer comes bundled with a Python client. The existing client was not sufficiently maintained or tested as development of Solr progressed, and committers felt that the code was not up to our usual high standards of release. <span class="anchor" id="line-7"></span><span class="anchor" id="line-8"></span></p><p class="line867">
</p><h2 id="solrpy">solrpy</h2>
<span class="anchor" id="line-9"></span><span class="anchor" id="line-10"></span><p class="line867"><a class="http" href="http://pypi.python.org/pypi/solrpy/">solrpy</a> is available at The Python Package Index so you should be able to: <span class="anchor" id="line-11"></span><span class="anchor" id="line-12"></span><span class="anchor" id="line-13"></span></p><pre>easy_install solrpy</pre><span class="anchor" id="line-14"></span><p class="line874">Or you can check out the source code and: <span class="anchor" id="line-15"></span><span class="anchor" id="line-16"></span><span class="anchor" id="line-17"></span></p><pre>python setup.py install</pre><span class="anchor" id="line-18"></span><span class="anchor" id="line-19"></span><p class="line867">
</p><h2 id="PySolr">PySolr</h2>
<span class="anchor" id="line-20"></span><span class="anchor" id="line-21"></span><p class="line874">There is a independent "pysolr" project available ... <span class="anchor" id="line-22"></span><a class="http" href="http://code.google.com/p/pysolr/">http://code.google.com/p/pysolr/</a> <span class="anchor" id="line-23"></span><span class="anchor" id="line-24"></span></p><p class="line862">And <a class="http" href="http://bitbucket.org/cogtree/python-solr/overview/">Python Solr</a>, And enhanced version of pysolr that supports pagination and batch operations. <span class="anchor" id="line-25"></span><span class="anchor" id="line-26"></span></p><p class="line867">
</p><h2 id="insol">insol</h2>
<span class="anchor" id="line-27"></span><span class="anchor" id="line-28"></span><p class="line874">Another independent Solr API, focused on easy of use in large scale production enviroments, clean and fast, still in development <span class="anchor" id="line-29"></span><span class="anchor" id="line-30"></span></p><p class="line867"><a class="http" href="http://github.com/mdomans/insol">http://github.com/mdomans/insol</a> <span class="anchor" id="line-31"></span><span class="anchor" id="line-32"></span></p><p class="line867">
</p><h2 id="sunburnt">sunburnt</h2>
<span class="anchor" id="line-33"></span><span class="anchor" id="line-34"></span><p class="line874">Sunburnt is an actively-developed Solr library, both for inserting and querying documents. Its development has aimed particularly at making the Solr API accessible in a Pythonic style. Sunburnt is in active use on several internet-scale sites. <span class="anchor" id="line-35"></span><span class="anchor" id="line-36"></span></p><p class="line867"><a class="http" href="http://pypi.python.org/pypi/sunburnt">http://pypi.python.org/pypi/sunburnt</a> <span class="anchor" id="line-37"></span><span class="anchor" id="line-38"></span></p><p class="line867"><a class="http" href="http://github.com/tow/sunburnt">http://github.com/tow/sunburnt</a> <span class="anchor" id="line-39"></span><span class="anchor" id="line-40"></span></p><p class="line867">
</p><h2 id="Using_Solr.27s_Python_output">Using Solr's Python output</h2>
<span class="anchor" id="line-41"></span><p class="line862">Solr has an optional Python response format that extends its <a href="/solr/SolJSON">JSON output</a> in the following ways to allow the response to be safely eval'd by Python's interpreter: <span class="anchor" id="line-42"></span></p><ul><li>true and false changed to True and False <span class="anchor" id="line-43"></span></li><li>Python unicode strings used where needed <span class="anchor" id="line-44"></span></li><li>ASCII output (with unicode escapes) for less error-prone interoperability <span class="anchor" id="line-45"></span></li><li>newlines escaped <span class="anchor" id="line-46"></span></li><li>null changed to None <span class="anchor" id="line-47"></span><span class="anchor" id="line-48"></span></li></ul><p class="line874">Here is a simple example of how one may query Solr using the Python response format: <span class="anchor" id="line-49"></span><span class="anchor" id="line-50"></span></p><p class="line867"><span class="anchor" id="line-51"></span><span class="anchor" id="line-52"></span><span class="anchor" id="line-53"></span><span class="anchor" id="line-54"></span><span class="anchor" id="line-55"></span><span class="anchor" id="line-56"></span><span class="anchor" id="line-57"></span><span class="anchor" id="line-58"></span><span class="anchor" id="line-59"></span><span class="anchor" id="line-60"></span></p><pre>from urllib2 import *
conn = urlopen('http://localhost:8983/solr/select?q=iPod&wt=python')
rsp = eval( conn.read() )
print "number of matches=", rsp['response']['numFound']
#print out the name field for each returned document
for doc in rsp['response']['docs']:
print 'name field =', doc['name']</pre><span class="anchor" id="line-61"></span><span class="anchor" id="line-62"></span><p class="line874">With Python 2.6 you can use the literal_eval function instead of eval. This only evaluates "safe" syntax for the built-in data types and not any executable code: <span class="anchor" id="line-63"></span><span class="anchor" id="line-64"></span><span class="anchor" id="line-65"></span><span class="anchor" id="line-66"></span></p><pre>import ast
rsp = ast.literal_eval(conn.read())</pre><span class="anchor" id="line-67"></span><span class="anchor" id="line-68"></span><p class="line867">
</p><h2 id="Using_normal_JSON">Using normal JSON</h2>
<span class="anchor" id="line-69"></span><span class="anchor" id="line-70"></span><p class="line862">Using <tt>eval</tt> is generally considered bad form and dangerous in Python. In theory if you trust the remote server it is okay, but if something goes wrong it means someone can run arbitrary code on your server (attacking eval is very easy). <span class="anchor" id="line-71"></span><span class="anchor" id="line-72"></span></p><p class="line862">It would be better to use a Python JSON library like <a class="http" href="http://undefined.org/python/#simplejson">simplejson</a>. It would look like: <span class="anchor" id="line-73"></span><span class="anchor" id="line-74"></span></p><p class="line867"><span class="anchor" id="line-75"></span><span class="anchor" id="line-76"></span><span class="anchor" id="line-77"></span><span class="anchor" id="line-78"></span><span class="anchor" id="line-79"></span><span class="anchor" id="line-80"></span></p><pre>from urllib2 import *
import simplejson
conn = urlopen('http://localhost:8983/solr/select?q=iPod&wt=json')
rsp = simplejson.load(conn)
...</pre><span class="anchor" id="line-81"></span><span class="anchor" id="line-82"></span><p class="line874">Safer, and as you can see, easy. <span class="anchor" id="line-83"></span></p><hr><p class="line874"> <span class="anchor" id="line-84"></span><span class="anchor" id="line-85"></span><span class="anchor" id="bottom"></span></p></div>
<!-- Django haystack --------->
<h1>For Django developers ...... Use Django Application</h1>
<div class="art-post-body">
<div class="art-post-inner">
<h2 class="art-postheader"> Haystack for Django</h2>
<div class="art-postcontent">
<!-- article-content -->
<div class="art-article"><p>Haystack provides modular search for Django. It features a unified, familiar API that allows you to plug in different search backends (such as Solr, Whoosh, Xapian, etc.) without having to modify your code.</p>
<p><a href="http://docs.haystacksearch.org/dev/toc.html">http://docs.haystacksearch.org/dev/toc.html</a></p>
</div>
<!-- /article-content -->
</div>
<div class="cleared"></div>
<div class="art-postmetadatafooter">
<div class="art-postfootericons art-metadata-icons">
</div>
</div>
</div>
</div>
<h3> Ubuntu users follow this link to install solr and access it through haystack </h3>
http://yuji.wordpress.com/2011/08/18/installing-solr-and-django-haystack-on-ubuntu-with-openjdk/
<br/><br/>Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-8511780156263103084.post-30862991292803761342011-10-19T03:26:00.000-07:002011-10-19T03:26:21.839-07:00Comet with Django and Ajax push engine ( APE )<div dir="ltr" style="text-align: left;" trbidi="on">
<div id="content">
<div class="post-33 post type-post status-publish format-standard hentry category-django" id="post-33">
<div class="post-header">
<div class="tags">
</div>
<h1>
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="font-weight: normal;">Recently I have implemented APE in my project . Before APE i was using continuous ajax call that is obviously very bad idea... in APE each user has its own pubid , channel , sessId . Channel is used for sending message to another user or vice-versa....</span></span></h1>
</div>
<div class="entry clear">
Here....I just slapped together a really basic message posting app. Just a message with a name and a timestamp.<br />
<br />
<script src="http://gist.github.com/417484.js">
</script><link href="https://gist.github.com/stylesheets/gist/embed.css" rel="stylesheet"></link><br />
<div class="gist" id="gist-417484">
<div class="gist-file">
<div class="gist-data gist-syntax">
<div class="gist-highlight">
<pre><div class="line" id="LC1">
<span class="c"># Here's the model:</span></div>
<div class="line" id="LC2">
<span class="k">class</span> <span class="nc">Message</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span></div>
<div class="line" id="LC3">
<span class="n">msg</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">(</span><span class="s">'Message'</span><span class="p">)</span></div>
<div class="line" id="LC4">
<span class="n">timestamp</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="s">'Timestamp'</span><span class="p">,</span> <span class="n">auto_now_add</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></div>
<div class="line" id="LC5">
<span class="n">posted_by</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span></div>
<div class="line" id="LC6">
</div>
<div class="line" id="LC7">
<span class="c"># And here's the view:</span></div>
<div class="line" id="LC8">
<span class="k">def</span> <span class="nf">show_messages</span><span class="p">(</span><span class="n">request</span><span class="p">):</span></div>
<div class="line" id="LC9">
<span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span><span class="p">:</span></div>
<div class="line" id="LC10">
<span class="n">new_msg</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="n">msg</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'msg'</span><span class="p">],</span></div>
<div class="line" id="LC11">
<span class="n">posted_by</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'posted_by'</span><span class="p">])</span></div>
<div class="line" id="LC12">
<span class="n">new_msg</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
<div class="line" id="LC13">
<span class="n">messages</span> <span class="o">=</span> <span class="n">Message</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span></div>
<div class="line" id="LC14">
<span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'messages.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">'messages'</span><span class="p">:</span> <span class="n">messages</span><span class="p">})</span></div>
</pre>
</div>
</div>
<div class="gist-meta">
<a href="https://gist.github.com/raw/417484/05a65629971db0bf186036541c2830e7dd0c0e7c/alttw_comet_django_ape_1.py" style="float: right;">view raw</a>
<a href="https://gist.github.com/417484#file_alttw_comet_django_ape_1.py" style="color: #666666; float: right; margin-right: 10px;">alttw_comet_django_ape_1.py</a>
<a href="https://gist.github.com/417484">This Gist</a> brought to you by <a href="http://github.com/">GitHub</a>.
</div>
</div>
</div>
And here’s the template:<br />
<script src="http://gist.github.com/417492.js">
</script><link href="https://gist.github.com/stylesheets/gist/embed.css" rel="stylesheet"></link><br />
<div class="gist" id="gist-417492">
<div class="gist-file">
<div class="gist-data gist-syntax">
<div class="gist-highlight">
<pre><div class="line" id="LC1">
<span class="nt"><style </span><span class="na">type=</span><span class="s">'text/css'</span><span class="nt">></span></div>
<div class="line" id="LC2">
<span class="nt">div</span><span class="nc">.message</span> <span class="p">{</span></div>
<div class="line" id="LC3">
<span class="k">background</span><span class="o">:</span> <span class="m">#DDDDDD</span><span class="p">;</span></div>
<div class="line" id="LC4">
<span class="k">margin</span><span class="o">:</span> <span class="m">10px</span><span class="p">;</span></div>
<div class="line" id="LC5">
<span class="k">padding</span><span class="o">:</span> <span class="m">10px</span><span class="p">;</span></div>
<div class="line" id="LC6">
<span class="p">}</span></div>
<div class="line" id="LC7">
<span class="nt"></style></span></div>
<div class="line" id="LC8">
<span class="nt"><div</span> <span class="na">id=</span><span class="s">"current_messages"</span><span class="nt">></span></div>
<div class="line" id="LC9">
{% for message in messages %}</div>
<div class="line" id="LC10">
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"message"</span><span class="nt">></span></div>
<div class="line" id="LC11">
{{ message.msg }}<span class="nt"><br</span> <span class="nt">/></span></div>
<div class="line" id="LC12">
Posted by: <span class="nt"><strong></span>{{ message.posted_by }}<span class="nt"></strong><br</span> <span class="nt">/></span></div>
<div class="line" id="LC13">
on: {{ message.timestamp }}</div>
<div class="line" id="LC14">
<span class="nt"></div></span></div>
<div class="line" id="LC15">
{% empty %}</div>
<div class="line" id="LC16">
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"message"</span><span class="nt">></span></div>
<div class="line" id="LC17">
No Messages</div>
<div class="line" id="LC18">
<span class="nt"></div></span></div>
<div class="line" id="LC19">
{% endfor %}</div>
<div class="line" id="LC20">
<span class="nt"></div></span></div>
<div class="line" id="LC21">
<span class="nt"><div</span> <span class="na">id=</span><span class="s">"new_message"</span><span class="nt">></span></div>
<div class="line" id="LC22">
<span class="nt"><form</span> <span class="na">id=</span><span class="s">"msgform"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span></div>
<div class="line" id="LC23">
<span class="nt"><h2></span>Post a new message<span class="nt"></h2></span></div>
<div class="line" id="LC24">
Your Name: <span class="nt"><input</span> <span class="na">name=</span><span class="s">"posted_by"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="nt">/><br</span> <span class="nt">/></span></div>
<div class="line" id="LC25">
Your Message:<span class="nt"><br</span> <span class="nt">/></span></div>
<div class="line" id="LC26">
<span class="nt"><textarea</span> <span class="na">cols=</span><span class="s">"50"</span> <span class="na">rows=</span><span class="s">"10"</span> <span class="na">name=</span><span class="s">"msg"</span><span class="nt">></textarea><br</span> <span class="nt">/></span></div>
<div class="line" id="LC27">
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">value=</span><span class="s">"Submit Message"</span> <span class="nt">/></span></div>
<div class="line" id="LC28">
<span class="nt"></form></span></div>
<div class="line" id="LC29">
<span class="nt"></div></span></div>
</pre>
</div>
</div>
<div class="gist-meta">
<a href="https://gist.github.com/raw/417492/d13621846713a18f99f3e7379111ecce48a5905b/alttw_comet_django_ape_2.html" style="float: right;">view raw</a>
<a href="https://gist.github.com/417492#file_alttw_comet_django_ape_2.html" style="color: #666666; float: right; margin-right: 10px;">alttw_comet_django_ape_2.html</a>
<a href="https://gist.github.com/417492">This Gist</a> brought to you by <a href="http://github.com/">GitHub</a>.
</div>
</div>
</div>
<br />
Ok, so that’s the basic setup… Pretty boring, right? If Sue submits a message, Bob has to refresh his page to see it. So, we’re going to add APE into the mix to update Bob right away. Getting APE up running it pretty easy, just follow the instructions over at the <a href="http://www.ape-project.org/wiki/index.php/Main">APE Wiki</a>. We’ll be using APE’s <a href="http://www.ape-project.org/wiki/index.php/Inlinepush">inlinepush</a> server module. Just for good measure we’ll be tossing some jQuery in as well.<br />
So, the basic idea is we need to connect to a APE “channel” which we’ll use to receive updated messages. We’ll use jQuery to send the messages. So let’s start by using jQuery to submit the form, with the following script:<br />
<script src="http://gist.github.com/417495.js">
</script><link href="https://gist.github.com/stylesheets/gist/embed.css" rel="stylesheet"></link><br />
<div class="gist" id="gist-417495">
<div class="gist-file">
<div class="gist-data gist-syntax">
<div class="gist-highlight">
<pre><div class="line" id="LC1">
<span class="kd">function</span> <span class="nx">append_message</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div>
<div class="line" id="LC2">
<span class="nx">fields</span> <span class="o">=</span> <span class="nx">data</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">fields</span><span class="p">;</span></div>
<div class="line" id="LC3">
<span class="nx">message_str</span> <span class="o">=</span> <span class="nx">fields</span><span class="p">.</span><span class="nx">msg</span> <span class="o">+</span> <span class="s1">'\nPosted by: <strong>'</span></div>
<div class="line" id="LC4">
<span class="o">+</span> <span class="nx">fields</span><span class="p">.</span><span class="nx">posted_by</span> <span class="o">+</span> <span class="s1">'</strong> on: '</span></div>
<div class="line" id="LC5">
<span class="o">+</span> <span class="nx">fields</span><span class="p">.</span><span class="nx">timestamp</span><span class="p">;</span></div>
<div class="line" id="LC6">
<span class="nx">new_div</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'<div />'</span><span class="p">).</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">'message'</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">message_str</span><span class="p">);</span></div>
<div class="line" id="LC7">
<span class="nx">$</span><span class="p">(</span><span class="s1">'div#current_messages'</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="nx">new_div</span><span class="p">);</span></div>
<div class="line" id="LC8">
<span class="p">}</span></div>
<div class="line" id="LC9">
</div>
<div class="line" id="LC10">
<span class="nx">$</span><span class="p">(</span><span class="s1">'#msgform'</span><span class="p">).</span><span class="nx">submit</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div>
<div class="line" id="LC11">
<span class="nx">$</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">'/ajaxsubmit'</span><span class="p">,</span></div>
<div class="line" id="LC12">
<span class="p">{</span><span class="nx">posted_by</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s2">"input[name='posted_by']"</span><span class="p">).</span><span class="nx">val</span><span class="p">(),</span></div>
<div class="line" id="LC13">
<span class="nx">msg</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s2">"textarea[name='msg']"</span><span class="p">).</span><span class="nx">val</span><span class="p">()},</span></div>
<div class="line" id="LC14">
<span class="nx">append_message</span><span class="p">,</span> <span class="s1">'json'</span><span class="p">);</span></div>
<div class="line" id="LC15">
<span class="c1">//For brevity, we're just going to assume this always works</span></div>
<div class="line" id="LC16">
<span class="nx">$</span><span class="p">(</span><span class="s2">"textarea[name='msg']"</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="s1">''</span><span class="p">);</span></div>
<div class="line" id="LC17">
<span class="k">return</span> <span class="kc">false</span><span class="p">;</span></div>
<div class="line" id="LC18">
<span class="p">});</span></div>
</pre>
</div>
</div>
<div class="gist-meta">
<a href="https://gist.github.com/raw/417495/28de8183b34b9fdd78fef9fa2b8e475aa0f6d458/alttw_comet_django_ape_3.js" style="float: right;">view raw</a>
<a href="https://gist.github.com/417495#file_alttw_comet_django_ape_3.js" style="color: #666666; float: right; margin-right: 10px;">alttw_comet_django_ape_3.js</a>
<a href="https://gist.github.com/417495">This Gist</a> brought to you by <a href="http://github.com/">GitHub</a>.
</div>
</div>
</div>
<br />
And here are the updated views:<br />
<script src="http://gist.github.com/417499.js">
</script><link href="https://gist.github.com/stylesheets/gist/embed.css" rel="stylesheet"></link><br />
<div class="gist" id="gist-417499">
<div class="gist-file">
<div class="gist-data gist-syntax">
<div class="gist-highlight">
<pre><div class="line" id="LC1">
<span class="k">def</span> <span class="nf">show_messages</span><span class="p">(</span><span class="n">request</span><span class="p">):</span></div>
<div class="line" id="LC2">
<span class="n">messages</span> <span class="o">=</span> <span class="n">Message</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span></div>
<div class="line" id="LC3">
<span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'messages.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">'messages'</span><span class="p">:</span> <span class="n">messages</span><span class="p">})</span></div>
<div class="line" id="LC4">
</div>
<div class="line" id="LC5">
<span class="k">def</span> <span class="nf">ajaxsubmit</span><span class="p">(</span><span class="n">request</span><span class="p">):</span></div>
<div class="line" id="LC6">
<span class="n">new_msg</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="n">msg</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'msg'</span><span class="p">],</span></div>
<div class="line" id="LC7">
<span class="n">posted_by</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'posted_by'</span><span class="p">])</span></div>
<div class="line" id="LC8">
<span class="n">new_msg</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
<div class="line" id="LC9">
<span class="n">jsonified_msg</span> <span class="o">=</span> <span class="n">serializers</span><span class="o">.</span><span class="n">serialize</span><span class="p">(</span><span class="s">"json"</span><span class="p">,</span> <span class="p">[</span><span class="n">new_msg</span><span class="p">])</span></div>
<div class="line" id="LC10">
<span class="c"># Again, we're just going to assume this always works</span></div>
<div class="line" id="LC11">
<span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">jsonified_msg</span><span class="p">,</span> <span class="n">mimetype</span><span class="o">=</span><span class="s">'application/javascript'</span><span class="p">)</span></div>
</pre>
</div>
</div>
<div class="gist-meta">
<a href="https://gist.github.com/raw/417499/d7e1f58c85d5ea6a2bcfc89c8197ea3f9331a29b/alttw_comet_django_ape_4.py" style="float: right;">view raw</a>
<a href="https://gist.github.com/417499#file_alttw_comet_django_ape_4.py" style="color: #666666; float: right; margin-right: 10px;">alttw_comet_django_ape_4.py</a>
<a href="https://gist.github.com/417499">This Gist</a> brought to you by <a href="http://github.com/">GitHub</a>.
</div>
</div>
</div>
<br />
And so with that, we can now submit messages without reloading the page. Any other users, however, would need to refresh to see the new messages. That’s where APE comes in. Here’s the APE client code (along with the updated append_message function):<br />
<script src="http://gist.github.com/417503.js">
</script><link href="https://gist.github.com/stylesheets/gist/embed.css" rel="stylesheet"></link><br />
<div class="gist" id="gist-417503">
<div class="gist-file">
<div class="gist-data gist-syntax">
<div class="gist-highlight">
<pre><div class="line" id="LC1">
<span class="kd">var</span> <span class="nx">client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">APE</span><span class="p">.</span><span class="nx">Client</span><span class="p">();</span></div>
<div class="line" id="LC2">
<span class="nx">client</span><span class="p">.</span><span class="nx">load</span><span class="p">();</span></div>
<div class="line" id="LC3">
</div>
<div class="line" id="LC4">
<span class="nx">client</span><span class="p">.</span><span class="nx">addEvent</span><span class="p">(</span><span class="s1">'load'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div>
<div class="line" id="LC5">
<span class="nx">posted_by</span> <span class="o">=</span> <span class="nx">prompt</span><span class="p">(</span><span class="s1">'Your name?'</span><span class="p">);</span></div>
<div class="line" id="LC6">
<span class="nx">client</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">start</span><span class="p">({</span><span class="s2">"name"</span><span class="o">:</span> <span class="nx">posted_by</span><span class="p">});</span></div>
<div class="line" id="LC7">
<span class="nx">$</span><span class="p">(</span><span class="s2">"input[name='posted_by']"</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="nx">posted_by</span><span class="p">);</span></div>
<div class="line" id="LC8">
<span class="p">});</span></div>
<div class="line" id="LC9">
</div>
<div class="line" id="LC10">
<span class="nx">client</span><span class="p">.</span><span class="nx">addEvent</span><span class="p">(</span><span class="s1">'ready'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div>
<div class="line" id="LC11">
<span class="c1">//Once APE is ready, join the messages channel and wait for new messages</span></div>
<div class="line" id="LC12">
<span class="nx">client</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">'messages'</span><span class="p">);</span></div>
<div class="line" id="LC13">
<span class="nx">client</span><span class="p">.</span><span class="nx">onRaw</span><span class="p">(</span><span class="s1">'postmsg'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">raw</span><span class="p">,</span> <span class="nx">pipe</span><span class="p">)</span> <span class="p">{</span></div>
<div class="line" id="LC14">
<span class="nx">append_message</span><span class="p">(</span><span class="nx">raw</span><span class="p">.</span><span class="nx">data</span><span class="p">);</span></div>
<div class="line" id="LC15">
<span class="p">});</span></div>
<div class="line" id="LC16">
<span class="p">});</span></div>
<div class="line" id="LC17">
</div>
<div class="line" id="LC18">
<span class="kd">function</span> <span class="nx">append_message</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div>
<div class="line" id="LC19">
<span class="nx">message_str</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">msg</span> <span class="o">+</span> <span class="s1">'\nPosted by: <strong>'</span></div>
<div class="line" id="LC20">
<span class="o">+</span> <span class="nx">data</span><span class="p">.</span><span class="nx">posted_by</span> <span class="o">+</span> <span class="s1">'</strong> on: '</span></div>
<div class="line" id="LC21">
<span class="o">+</span> <span class="nx">data</span><span class="p">.</span><span class="nx">timestamp</span><span class="p">;</span></div>
<div class="line" id="LC22">
<span class="nx">new_div</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'<div>'</span><span class="p">).</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">'message'</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">message_str</span><span class="p">);</span></div>
<div class="line" id="LC23">
<span class="nx">$</span><span class="p">(</span><span class="s1">'div#current_messages'</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="nx">new_div</span><span class="p">);</span></div>
<div class="line" id="LC24">
<span class="p">}</span></div>
</pre>
</div>
</div>
<div class="gist-meta">
<a href="https://gist.github.com/raw/417503/0452a610ef6bc5f375fb5a61dd46a45748db472d/alttw_comet_django_ape_5.js" style="float: right;">view raw</a>
<a href="https://gist.github.com/417503#file_alttw_comet_django_ape_5.js" style="color: #666666; float: right; margin-right: 10px;">alttw_comet_django_ape_5.js</a>
<a href="https://gist.github.com/417503">This Gist</a> brought to you by <a href="http://github.com/">GitHub</a>.
</div>
</div>
</div>
<br />
And here’s the updated view to send new posts to APE:<br />
<script src="http://gist.github.com/417505.js">
</script><link href="https://gist.github.com/stylesheets/gist/embed.css" rel="stylesheet"></link><br />
<div class="gist" id="gist-417505">
<div class="gist-file">
<div class="gist-data gist-syntax">
<div class="gist-highlight">
<pre><div class="line" id="LC1">
<span class="k">def</span> <span class="nf">ajaxsubmit</span><span class="p">(</span><span class="n">request</span><span class="p">):</span></div>
<div class="line" id="LC2">
<span class="n">new_msg</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="n">msg</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'msg'</span><span class="p">],</span></div>
<div class="line" id="LC3">
<span class="n">posted_by</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'posted_by'</span><span class="p">])</span></div>
<div class="line" id="LC4">
<span class="n">new_msg</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
<div class="line" id="LC5">
<span class="c"># Again, we're just going to assume this always works</span></div>
<div class="line" id="LC6">
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[{</span><span class="s">'cmd'</span><span class="p">:</span> <span class="s">'inlinepush'</span><span class="p">,</span></div>
<div class="line" id="LC7">
<span class="s">'params'</span><span class="p">:</span> <span class="p">{</span></div>
<div class="line" id="LC8">
<span class="s">'password'</span><span class="p">:</span> <span class="n">settings</span><span class="o">.</span><span class="n">APE_PASSWORD</span><span class="p">,</span></div>
<div class="line" id="LC9">
<span class="s">'raw'</span><span class="p">:</span> <span class="s">'postmsg'</span><span class="p">,</span></div>
<div class="line" id="LC10">
<span class="s">'channel'</span><span class="p">:</span> <span class="s">'messages'</span><span class="p">,</span></div>
<div class="line" id="LC11">
<span class="s">'data'</span><span class="p">:</span> <span class="p">{</span></div>
<div class="line" id="LC12">
<span class="s">'msg'</span><span class="p">:</span> <span class="n">new_msg</span><span class="o">.</span><span class="n">msg</span><span class="p">,</span></div>
<div class="line" id="LC13">
<span class="s">'posted_by'</span><span class="p">:</span> <span class="n">new_msg</span><span class="o">.</span><span class="n">posted_by</span><span class="p">,</span></div>
<div class="line" id="LC14">
<span class="s">'timestamp'</span><span class="p">:</span> <span class="n">new_msg</span><span class="o">.</span><span class="n">timestamp</span></div>
<div class="line" id="LC15">
<span class="p">}</span></div>
<div class="line" id="LC16">
<span class="p">}</span></div>
<div class="line" id="LC17">
<span class="p">}]</span></div>
<div class="line" id="LC18">
<span class="n">url</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">APE_SERVER</span> <span class="o">+</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">quote</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">cmd</span><span class="p">))</span></div>
<div class="line" id="LC19">
<span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">)</span></div>
<div class="line" id="LC20">
<span class="c"># Updating the message is handled by APE, so just return an empty 200</span></div>
<div class="line" id="LC21">
<span class="k">return</span> <span class="n">HttpResponse</span><span class="p">()</span></div>
</pre>
</div>
</div>
<div class="gist-meta">
<a href="https://gist.github.com/raw/417505/d6d5b9edf38f5b3c49219c23c1c06677105daed3/alttw_comet_django_ape_6.py" style="float: right;">view raw</a>
<a href="https://gist.github.com/417505#file_alttw_comet_django_ape_6.py" style="color: #666666; float: right; margin-right: 10px;">alttw_comet_django_ape_6.py</a>
<a href="https://gist.github.com/417505">This Gist</a> brought to you by <a href="http://github.com/">GitHub</a>.
</div>
</div>
</div>
You’ll notice I’ve added two settings to settings.py:<br />
<pre>APE_PASSWORD = 'testpasswd'
APE_SERVER = 'http://ape-test.local:6969/?'</pre>
<pre>
</pre>
And that’s all there is to it! Like I said earlier, this is a very incomplete application, and a lot of the “boilerplate” work is left as an exercise for the reader. It should be more than enough to get you on your way though. Perhaps in a later post I’ll move the APE update from inside the view to a post_save signal on the model itself. That’s for another time though… UPDATE: Well, another time is today. Check out <a href="http://www.alittletothewright.com/2010/01/apedjango-and-signals/">this post</a> about adding a signal handler.<br />
<br />
Reference : <a href="http://www.alittletothewright.com/index.php/2010/01/comet-with-django-and-ape/">http://www.alittletothewright.com/index.php/2010/01/comet-with-django-and-ape/</a><br />
<br />
If any question or error then feel free to comment here........before that i would like to write some problems/guidelines/alternative solution that i had used in my project.....<br />
<br />
1) Ready event not working when page refresh :<br />
-> yes.........to get it working you have to use session to store each user .........OR ........without session u can append random no to name when calling client.core.start that will load new user each time .<br />
<br />
2) Implement chat...<br />
-> To implement chat u need to register new channel for each user ...when somebody wants to chat he/she will send message to that channel using pipe.send method and the target user will check for new msg using client.onRaw method.......</div>
</div>
<div id="respond">
<form action="http://www.alittletothewright.com/wp-comments-post.php" id="commentform" method="post">
<input id="comment_post_ID" name="comment_post_ID" type="hidden" value="33" />
<input id="comment_parent" name="comment_parent" type="hidden" value="0" />
</form>
</div>
</div>
</div>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8511780156263103084.post-34331696771008648382011-10-19T02:34:00.000-07:002011-10-19T03:03:35.415-07:00Pagination in Django is now simple with this application<b>Source code :<b><a href="http://code.google.com/p/django-pagination/">Download</a><br /><br /><div><br /><embed src="http://blip.tv/play/AcH4UAA" type="application/x-shockwave-flash" width="550" height="410" allowscriptaccess="always" allowfullscreen="true"></embed><br /></div></b></b>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-7145451970355554472011-09-02T04:08:00.000-07:002011-09-02T04:19:22.902-07:00Django - Send and retrieve data from method(in views) with jquery ajax callIn django Some guys experience "csrfmiddlewhere" problem while passing single data to method without form using jquery ajax post method...
<br />Here is a solution for that.....
<br />
<br />put {% csrf_token %} in any where in page........preferebly in empty form.
<br />
<br />
<br />in your javascript file put this code and change it according to your ids......
<br />
<br />
<br />
<br />
<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6elXqD-J3B0sQX4NGhAnynY5E30BRhIOMbISQXXS7XBbjBy-6ViexjNc4a5WaKknWfZNFsU-UZGbxTMTm_YkykN2qk6uwvxKiFARvwoArhaWpkgegR5pEeRzO_JTslyea9BDfqQmsjF8/s1600/csrf.JPG"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 254px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6elXqD-J3B0sQX4NGhAnynY5E30BRhIOMbISQXXS7XBbjBy-6ViexjNc4a5WaKknWfZNFsU-UZGbxTMTm_YkykN2qk6uwvxKiFARvwoArhaWpkgegR5pEeRzO_JTslyea9BDfqQmsjF8/s320/csrf.JPG" alt="" id="BLOGGER_PHOTO_ID_5647720047961179282" border="0" /></a>
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8511780156263103084.post-3734168792078352752011-08-24T03:06:00.000-07:002011-08-24T03:22:14.962-07:00Put Like button of facebook with counts.<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr0UuUAdFD9YaGV4nDdzC2ohSSnPXcU4gyEatFu9mqKsbNOJXmxzHCo31z-dq4cMI8fQPiunTZL7S6k-HYFEJgYiZ4VnxYaMZytz0b99Dt-AVNGc8Bcgz_TGxaDtmzhXq390SiVIhMMpU/s1600/fb_like.JPG"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 256px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr0UuUAdFD9YaGV4nDdzC2ohSSnPXcU4gyEatFu9mqKsbNOJXmxzHCo31z-dq4cMI8fQPiunTZL7S6k-HYFEJgYiZ4VnxYaMZytz0b99Dt-AVNGc8Bcgz_TGxaDtmzhXq390SiVIhMMpU/s320/fb_like.JPG" alt="" id="BLOGGER_PHOTO_ID_5644364553620311058" border="0" /></a>
<br /><br/>
<br /><br/>
<br /><br/>
<br /><br/>
<br /><br/>
<br /><br/>
<br /><br/><br/><br/><br/><h3>First of all go to this link and register yourself . you will get "APP_ID" .write it on notepad.</h3><a href="http://www.facebook.com/developers">http://www.facebook.com/developers</a>
<br />
<br />Now put this(image) code in your blog/website wherever you would like to.....
<br />
<br />Thats all.............Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8511780156263103084.post-76551660708727811862010-02-02T18:54:00.000-08:002010-02-02T18:57:19.980-08:00character to ascii bits#include <stdio.h><br />#include<conio.h><br />void main()<br />{<br /> int i,arr[8],j;<br /> char ch;<br /> FILE *fp; // file pointer initialization<br /> fp=fopen("c:\\priyu.txt","r"); //open in read mode<br /> ch=fgetc(fp);<br /> clrscr();<br /> // printf("enter character :");<br /> // scanf("%c",&ch);<br /> printf("%c=%d in binary:",ch,ch);<br /> for(i = 7,j=0; i>=0; i--,j++)<br /> {<br /> // printf("\n%d",(1<<i)&ch);<br /> //if not 0 then if or else<br /> if((1<<i)&ch) // Anding operation with character ascii bits<br /> {<br /> printf("1");<br /> arr[j]=1;<br /> }<br /> else<br /> {<br /> printf("0");<br /> arr[j]=0;<br /> }<br /> }<br /> printf("\n");<br /> for(j=0;j<=7;j++)<br /> {<br /> printf("%d",arr[j]);<br /> }<br /> //find first four bits<br /> printf("\nfirst four bits of character:");<br /> for(j=0;j<=3;j++)<br /> {<br /> printf("%d",arr[j]);<br /> }<br /> printf("\n");<br /> getch();<br />}Unknownnoreply@blogger.com4