Thursday, March 21, 2013

ASSIGNING A QUERY RESULT TO DJANGO FORMS AND ASSIGNING A CLASS TO A DJANGO FORM FIELD

In Django if you chose to make forms from the models. Its really easier in a sense that you don't need to describe a separate HTML defining each and every field. By calling some Django forms functions things can be easily validated so you no longer need POST's to fetch data from html field. Just post the form and the Django will take care of all the other stuff.

In this post i am going to describe how to assign jquery classes and filtered queries to forms field in order to assign them some filtered data before they are shown on screen.
So you need to navigate into the forms.py file and write a little code


class FormName(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(FormName, self).__init__(*args, **kwargs)


Now Assign the queryset or the jquery classes in init function
If  you want to assign a query set then write django query like the one given
self.fields['author'].queryset = Author.objects.filter(Books__title__icontains = "Beautiful")
If you want to assign a JQuery class then follow the code written below
self.fields['author'].widget.attrs['class'] = 'required'
The "required" class is the class that is written to validate that the field on which it is applied must not remain empty.


        ajax = true
        data = {}
        $('.required').each(function(){
            $(this).css('border', '1px solid eee');
            data[$(this).attr('author')] = $(this).val()
            if ($.trim($(this).val()) == "") {
                $(this).css('border', '2px solid red');
                ajax = false
            }
            else
            {
                $(this).css('border', '1px solid #A9A9A9');
            }
        })

This is the required class that is placed in my .js file. So using this way you can asssign a dataset and jquery classes to django forms field.

Saturday, March 9, 2013

INSTALLING PIP ON WINDOWS

Sometimes you need to use utilities of Linux on windows as well. So if you want to execute commands in the command prompt just like linux shell follow this post.


1. Download and uncompress the latest pip version from here






2. Now you need to download the installer for Windows so you need to follow this link and downloa exe file and install it.




3. Now you need to copy the pip folder's content(not the folder) in the folder where your python is installed. A path like C:\Python2X(X means the version of your python) directory.


4. Add your python c:\Python2x\Scripts to the path. See my this post to get an idea how to add environment variables to path

You are good to go. Type pip install <package_name> and you are done.
Happy Linux life in Windows...:)








Sunday, March 3, 2013

INTALLING DJANGO FROM PYTHON (the smart way)

To Install Django you don't need to download it from somewhere else. If you have Python installed in your PC you can do this swiftly and easily by following this post.

First you need to move into directory where your Python is installed by typing the command in command prompt
cd C:/Python2.X 

Now you will see a Scripts directory in Python folder. You must have Scripts in your Python Path. You can do this by adding semicolon(;) and appending C:\PythonX\Scripts just like you did while installing Python. To see how to set environement varaibles check the first screen-shot of my THIS post. So going on type the command
cd Scripts
Now type the command to install Django
easy_install django

If you follow any issue while following this post then you must first install setuptools for windows. You can read more about it here