Sunday, April 7, 2013

ADDING AUTOCOMPLETE, AUTOFOCUS, PLACEHOLDER AND OTHER ATTRIBUTES TO DJANGO FORMS

In HTML its easy to give just an attribute to a tag to set autofocus or autocomplete on/off. In Django forms the way around is a little different from the traditional way.
In HTML you can set a placeholder like
<input name = "name" placeholder = "Awais Ali" type = "text" />
So in Django you can do this by
self.fields['Name'].widget.attrs['autofocus']  = 'on'
You can also assign auto-complete in a similar fashion
self.fields['Name'].widget.attrs['autofocus']  = 'off'
Similarly place-holder can be set like this
self.fields['Name'].widget.attrs['placeholder']  = u'Your Name'
This can also be set using  dictionary by giving key, value like this
self.fields['Name'].widget.attrs({'placeholder':'Your Name'}) 

So over-all if you want every field to have some property or value then here is the code how to do it


 def __init__(self, *args, **kwargs):
        super(LoginForm,self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs['autocomplete'] = 'off'
        self.fields['username'].widget.attrs['autofocus'] = 'on'
        self.fields['password'].widget.attrs['autocomplete'] = 'off'
 
    username = forms.CharField(label=_("Username"), max_length=30, widget=forms.TextInput())
    password = forms.CharField(label=_("Password"), widget=forms.PasswordInput(render_value=False))


So after putting this code in your forms.py you would achieve the required functionality. Similarly you can go on adding HTML attributes form here whatever you like to add for example:
read-only, disabled, multiple etc.

Feel free to ask if you have any queries in the comment below.


Wednesday, April 3, 2013

CONFIGURING DJANGO, APACHE AND MOD_WSGI ON CENTOS (Freshly Installed)

A lot of people have been asking me about the django configuration and its installation. I published a post regarding its installation. Its quite easy because you don't have to do anything you just need to run a script and its installed. The next thing is configuring django on Cent OS. So i thought of writing a post for newbie's to Linux and django environment to make them configure django on a freshly installed Cent OS

If the Cent OS is freshly installed then it would not have pip and easy_install. So first we need to install these tools. Its not mandatory but pip is used for installing and managing Python packages. Easy_install   is used for downloading, installing, uninstalling, upgrading and building python packages easily.



Easy_Install:wget http://peak.telecommunity.com/dist/ez_setup.pypython ez_setup.py
PIP
easy_install pip 


Now that you have installed the main python tools. Its time for some dependencies to be installed 

It will save a lot of time if they are installed on this stage and prevent errors that will pop in future during the process.
Dependencies:
  • yum install httpd-devel
  • yum install make
  • yum install gcc(compiler to make/complie C files)
  • or alternatively yum install python-devel
  • or alternatively yum install gcc-c++
  • yum install MySQL-python 

Now that dependencies are installed we are good to go.
INTALL DJANGO
pip install django
INSTALL MySQL
yum install mysql mysql-server 
INSTALL HTTPD   
yum install httpd. Type the command service httpd restart to check the status you can type service httpd status. 
INSTALL MOD_WSGI    yum install mod_wsgi 
If this command does not work properly then install manually through wget command



      tar xzf mod_wsgi-2.6.tar.gz
      cd mod_wsgi-2.6
      ./configure --with-python=/usr/bin/python2.5
      make
      sudo make install


Now change mode of wsgi file so that it can be called from httpd


chmod 755 /etc/httpd/modules/mod_wsgi.so

Now we have to edit httpd.conf
vi /etc/httpd/conf/httpd.conf 
Edit and add the given line in LoadModule section

LoadModule wsgi_module modules/mod_wsgi.so
Add WSGIScriptAlias
WSGIScriptAlias / /var/www/sitefolder/<project>/wsgi.py 

Paste these few lines that are necessary to show your required folders. I have to do this in my case beacause i have similar folders in my site
AliasMatch ^/([^/]*\.css) /var/www/sitefolder/<project>/static/css/$1AliasMatch ^/([^/]*\.[jpg|gif|png]) /var/www/<sitefolder/<project>/static/images/$1 Alias /media/   /var/www/wtoolgui/sitefolder/<project>/media/Alias /static/  /var/www/wtoolgui/sitefolder/<project>/static/  <Directory /var/www/<project>/static/ >    Order deny,allow    Allow from all</Directory>  <Directory /var/www/<project>/media/ >    Order deny,allow    Allow from all</Directory> WSGIRestrictStdout Off 

Now that you have done all the settings you need to make a wsgi file and place it into your project directory
vi /var/www/<project>/wsgi.py

Paste the given code into the file
import osimport sys path = '/var/www/<project>/'if path not in sys.path:    sys.path.append(path) os.environ['DJANGO_SETTINGS_MODULE'] = '<project>.settings' import django.core.handlers.wsgiapplication = django.core.handlers.wsgi.WSGIHandler() 
Now you are all set up to make a django project. Make models after creating app and then run the command python manage.py syncdb to add those tables in the database.

Enjoy Python and Django and make applications.
Feel free to ask any queries in the comments below.