07 July 2009

Django using Eclipse with pydev extension

This article helps you configure eclipse so that you do web app development using django framework. Eclipse can be used for python programming using pydev plugin. The same plugin will be used for working with django framework also.

I assume you already have pydev plug-in for your eclipse. If not you can download it here. I assume that you have already installed django. If not you can download it here. Django installation instructions are here.
  1. Create your django project folder using the command line method as illustrated in the django tutorial. I assume you have used the project name as "mysite". For reference, I will consider it as located at "documents/mysite".
  2. In eclipse go to windows>preferences and then select Pydev>Interpreter-Python. Select the python interpreter (python dot exe). In the System PYTHONPATH section, please select the django folder you installed. Click Apply. Then click OK.
  3. Create a pydev project called "mysite" with src folder. For reference I will assume the project folder will be at "eclipse_workspace/mysite".
  4. Copy the entire "mysite" folder from "documents/mysite" and paste it into "eclipse_workspace/mysite/src" folder. Refresh your view in eclipse. You should now be able to see mysite package and polls package in the eclipse package explorer.
  5. Lets set up some run configurations. Go to Run>Run Configurations... In the Run Configuration window, select Python Run on the left. Right click and select New. Name it as "manage_config". Select the project as "mysite" and Main Module as "manage.py". In "arguments" tab, add "runserver --noreload" in Program Arguments section. In "environments" tab, add variable DJANGO_SETTINGS_MODULE with value mysite.settings. You can add new variables by clicking on "New..." button. Now you can run the manage.py module, and this will start the django inbuilt development server. If you want to debug the app, run the manage.py module in debug mode. Just right click on manage.py and then select debug>debug configurations and select manage_config. Click on debug. Or right click on manage.py select debug as> python run, if you have only one run configuration defined for manage.py

  6. Create a package called "test" and a module called "test". Use this module to play with your Django API. Lets set up some run configurations for test module. Go to Run>Run Configurations... In the Run Configuration window, select Python Run on the left. Right click and select New. Name it as "test_config". Select the project as "mysite" and Main Module as "test.py".

    In "environments" tab, add variable DJANGO_SETTINGS_MODULE with value mysite.settings. You can add new variables by clicking on "New..." button. Now you can run the manage.py module, and this will start the django inbuild dev .

    Now you can run or debug the test.py module and play with the Django API to your hearts fill.

    Please feel free to test drive and let me know if you run into issues. This set up is pretty much independent of version numbers. If you have any specific problem with any specific version combination, please leave a comment.