Yawd website

may31

Using Aptana Studio 3 IDE for django applications - a "Hello world" tutorial

Aptana Studio 3 is a development IDE targeting the most to web developers. Being an Eclipse-based platform, it runs as standalone application -it utilizes the Eclipse RCP framework-, as well as a plugin inside your existing Eclipse installation. I recently have decided to test Aptana for developing Django applications and this article covers Aptana's basic functionality.

Installation

First of all we need to install Python. Python 3 was recently released, but version 3 is not supported by Django, so we will use 2.7.x found at http://www.python.org/download/. After Python is installed we need to setup Django.The latest Django version is 1.3 and it can be found here. Unrar the tarball, visit the extracted directory and run:
python setup.py install

To install Aptana 3 you only need to download the zip file from http://www.aptana.com/products/studio3/download -choose the standalone version-, extract it and you are ready to go; no installation required. Aptana comes with out of the box support for Django applications using the PyDev project, which is also available as a separate Eclipse plugin.

Configure Aptana

To work with Django, Aptana must be configured to recognise our local python installation. Open Aptana 3 and choose Window->Preferences. Navigate through the options at the left of the new window and visit PyDev -> Interpreter - Python. Figure 1 shows a screenshot of the 'Preferences' window. You can select 'Auto config' (Aptana will attemp to automatically locate your Python installation) or use the 'New...' button to manually navigate to your Python folder (for Windows users this will be the folder containing your python.exe file).

Configure Python interpreterFigure 1. Configure Python interpreter.

Hello world project

Now let's create a new project. Select File->New->Other. A new wizard window should open. Locate the 'PyDev Django Project' under the 'PyDev' directory and select 'Next'. When in the last wizard page, make sure 'Django 1.2 or later' is selected in the 'Django version' field and press 'Finish'.

Create a new 'HelloWorld' django projectFigure 2. The new Django project wizard.

Aptana might ask whether the new project should be associated to the 'PyDev perspective'. Choose 'Yes'. In the PyDev Package Explorer -located at the left of our workbench- you can see the project structure that PyDev generated. This is the exact same structure that the 'django-admin.py startproject mysite' command would have generated. For more information about the files and their usage see the official 'first Django app' tutorial.

Enable Django admin

Django comes with a built-in administration interface. To enable this interface open the settings.py file (it was automatically created by the PyDev wizard). You can find the file in Aptana's PyDev Package Explorer window at the left -these windows are called 'views' in Eclipse-. In settings.py locate the 'INSTALLED_APPS' variable and uncomment the 'django.contrib.admin' line (With my current setup, this is line 119 in settings.py).

settings.py
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

Now open the urls.py file using the PyDeve Package Explorer view. Uncomment lines no 4, 5 and 16. We do this to correlate the '/admin' URL of our project with Django's administrator website. Your urls.py file should look like the one of Figure 3.

Aptana's Python editor windowFigure 3. Aptana/PyDev's main Python editor window. Screenshot of the automatically generated urls.py file with enabled admin-site functionality.

Create the database

Django will automatically generate the database for us. In this example we use 'sqlite', a text-based database that the 'New PyDev Django wizard' configured for us back when creating the project. Right-click our project root in PyDev Package Explorer. You should see a 'Django' menuitem that opens a sub-menu (Figure 4). The options listed in this sub-menu will cover most -not to say all- of your work with django commands, as they wrap Django's manage.py and django-admin.py functionality. Therefore, to sync our database we only need to select Sync DB instead of opening a console and typing 'python manage.py syncdb' - which is a life-saver in Windows environments. After selecting the 'Sync DB' option the script will run and its output will be shown in the 'Console' view located at the bottom of Aptana's environment. The script will ask you to create a new account. Enter a username, password and e-mail as requested: this is our website's first administrator account!

Aptana's Python editor windowFigure 4. Aptana/PyDev Django commands.

Visit the administrator website

We now need to run our application. Right-click the project root and select Run As->PyDev: Django. Now a lightweight web server is running our HelloWorld application. Visit http://localhost:8000/admin and login using the credentials you entered when intializing the database. You should see the administration panel shown in Figure 5.

Django administrator interfaceFigure 5. Screenshot of Django's administrator interface.

Create the model

Our goal for this tutorial is to set a string value using Django's administrator panel and be able to see that value printed on our website's frontend! To do so, we first need to define a model. Models are part of Django's Object-relational mapping (ORM) implementation. In Aptana's PyDev Package Explorer window, right-click the project root element and select Django->Create Application. Name the new application 'print_string'. This will generate a new package inside our project's source folder and is equivalent to running Django's 'python manage.py startapp print_string' command. Double-click the new models.py file created in our new application's package and type in the following code:

models.py
from django.db import models

class HelloWorld(models.Model):
    string = models.CharField(max_length=255)

Register the application

Once we defined our model we need to allow the administrator interface to insert, edit and delete records of our model. To do so, right-click the 'print_string' package and select New->PyDev module. Name the new module 'admin'. Now we have created a new module inside our 'print_string' application. Open the admin.py module and insert the following code:

admin.py
from django.contrib import admin
from models import HelloWorld

admin.site.register(HelloWorld)

Finally, we need to let our Django project use the 'print_string application'. We simply open the main project's settings.py module, locate the 'INSTALLED_APPS' tuple and append the string 'print_string', just like we did with the 'django.contrib.admin' application.

Sync the database and restart web server

Before we can see the result of our code we need to make two more actions: Since we created a new model we first need to Sync DB (Right-click in project root->Django->Sync DB). This will create a new database table named 'print_string_helloworld' to store our string records. Now restart the webserver. This can be done through the 'Console' view at the bottom of Aptana's workbench. Click on the 'Display Selected Console' icon at the right of the Console view (the icon displays a PC monitor) and select the '-noreload' console. This brings up our web server console. Now press the 'restart current launch' of the console.

Insert & print a string

Visit http://localhost:8000/admin once again and now you should see a new fieldset named 'print_string'. Click on the 'Add' button of the 'HelloWorld' element and you should see a form for editing the model (Figure 6)! Use it to enter the string 'Hello Django World!' and press save.

The 'HelloWorld' record edit formFigure 6. The 'HelloWorld' record edit form.

To print our models to the front-end we first need to open the 'urls.py' module (found in the project source directory) and modify it to look as follows:
urls.py
from django.conf.urls.defaults import patterns, include, url
from django.views.generic import ListView
from print_string.models import HelloWorld
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$',  ListView.as_view(
        model=HelloWorld, template_name='index.html'
    ), name='home'),
    url(r'^admin/', include(admin.site.urls)),
)

The above example utilizes the class-based views introduced in Django 1.3.

Finally we will create a template that prints the output. Create a new folder named 'templates' inside the 'print_string' application. Right click the 'print_string' package and select New->Folder. Right click the new folder and create a new file inside it (New->File) named 'index.html'. By now your project tree should look like the one in Figure 7.

The complete 'HelloWorld' project treeFigure 7. The complete 'HelloWorld' project tree.

Open 'index.html' and type the following code, that iterates over our HelloWorld model records and simply prints the 'string' attribute

index.html
{% for hw in object_list %}
  {{hw.string}}<br/>
{% endfor %}

Finally visit http://localhost:8000 and your 'Hello Django World!' message is printed on the screen!

Hello Aptana/Django World!Figure 8. Hello Aptana/Django World!

That's it! Hope you enjoyed it and feel free to comment!

Meta

Published: May 31, 2011
Comments:  
Word Count: 1,367
Comments powered by Disqus