Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Sunday, May 2, 2010

Python IDE: the Pydev plugin for Eclipse

Choices for a good open source Python IDE are not many! Today I'll show you how to quickly have a basic setup working with Pydev for Eclipse. Since the last post about SFTP and FTP support in Eclipse. You already know it is my platform of choice.

Pydev is a product by Aptana which also provides the Aptana Studio software for working with web development (Ajax, Ruby, PHP, etc...).  The plugin as all the usual features you would expect from a normal IDE and also some goodies like Django integration. Instead of a lengthy discussion on the pros and cons of this setup. Let's just go ahead with installing and configuring it so you can judge for yourself.

Installing the Pydev plugin.

To install install the plugin you obviously need to first have Eclipse. I generally start with Eclipse Classic but that will pretty much depend on your preferences... From Eclipse follow theses steps to get the plugin:

  1. Go the Help menu and click Install New Software.
  2. On the work with text box insert http://pydev.org/updates and click Add.
  3. You  can insert a name for this update site and wait for Eclipse to check the available content.
  4. Select Pydev and hit Next two times.
  5. Accept the license agreement if that's ok with you and click Finish.
After restarting Eclipse the plugin should be installed and ready to be configured.

Configuring the Pydev plugin.

To get started on programming with Pydev you now need to tell it where to find the interpreter:
  1. Open the Preferences window. Go to Window then preferences.
  2. Next you expand Pydev and select Interpreter - Python.
  3. On the right pane you can select Auto Config if your system path is properly set. Otherwise you might have to select your interpreter and libraries manually.
This is all that is needed for a basic setup. You can start exploring you newly installed Python IDE. Any suggestions are welcome.

Sunday, April 18, 2010

Django whois using the subprocess module

Last month I wrote a post about writing a whois client. This time I want to investigate If it's possible to leverage your operating system for those kind of tasks or if you really need to reinvent the wheel.

So why did I wrote my own crappy whois client in the first place? As you may already have noticed from my blog it's because I want to use the whois client from within Django (web application framework) hence the need to have a python module that can do whois queries.

Ok so the need is pretty clear but there should be a way to call the whois client of my operating system from a python module instead. This would allow me to write less code and I'll probably end up with a more robust whois client. So what possibilities does Python offer to spawn a process and communicate with it?

Since version 2.4 there is the subprocess module. In essence this is exactly what I need. So let's see how we can use it from within Django to provide a web interface to the whois native client.

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
import subprocess

def whois(domain):
    p = subprocess.Popen(['whois', domain], stdout=subprocess.PIPE)
    answer = p.commmunicate()
    return answer

class WhoisForm(forms.Form):
    domainname = forms.CharField(max_length=100)

if 'whois' in request.POST:
    whois_form = WhoisForm(request.POST, prefix='whois')
    if whois_form.is_valid():
        domainname = whois_form.cleaned_data['domainname']
        answer = whois(domainname)
else:
    whois_form = WhoisForm( prefix='whois')

return render_to_response('index.html', {'whois_form': whois_form,
                                         'answer': answer,})

What happens in the code above is that we spawn the whois subprocess and pipe its output. This enables us to communicate with it and retrieve it's standard output in a variable. We can then pass along the output to the template for the user to view in his browser. Quick and easy!

Ok, so at this point I should be a happy man! I did reach my initial goal of not reinventing the wheel. However  I'm really not sure this is the way to go. Next time we will use the timeit module to see how this one compares to the python whois client regarding performance.

Friday, April 2, 2010

Multiple Django forms in the same view

When you want to handle multiple Django forms within the same view you need some extra coding as opposed to having a single form. This is needed because:

  • You want to identify which form has been submitted to your view.
  • And you don't want to confuse fields from one form with fields from another.
So let's start with identifying the form from your view. You'll first need to add some information to your template to distinguish between the two forms. This can be easily done by giving a unique name to each submit buttons. For the rest of this post we will assume that we have two forms: A and B.

<fieldset>
    <legend>Form A</legend>
    <form method="post">
        {{ A_form.as_p }}
        <input name="A" type="submit" value="Submit" />
    </form>
</fieldset>

<fieldset>
    <legend>Form B</legend>
    <form method="post">
        {{ B_form.as_p }}
        <input name="B" type="submit" value="Submit" />
    </form>
</fieldset>

Now that you uniquely identified both forms you can add some logic to your view to get to know which submit button as been pressed.


if 'A' in request.POST:
    # A form processing
elif 'B' in request.POST:
    # B_form processing

What happens above is that you search the submitted POST data dictionary for the name you gave to your submit buttons. That's all there is to it.

However if for some reason fields in the different forms you present to your users have the same names then you need to add a couple more lines of code to distinguish between them. From the django documentation you can borrow the strategy in using more than one formset in a view. The idea is to prefix each field name with a form identifier. I choose to use the same token for the prefix and for the submit button name.

The end result would be something like this:

class AForm(forms.Form):
    fieldname = forms.CharField(max_length=25)

class BForm(forms.Form):
    fieldname = forms.CharField(max_length=25)

if 'A' in request.POST:
    A_form = AForm(request.POST, prefix='A')
    B_form = BForm(prefix='B')
    # A form processing
elif 'B' in request.POST:
    B_form = BForm(request.POST, prefix='B')
    A_form = AForm(prefix='A')
    # B_form processing