I’ve been doing a bit of work with the Django framework lately, and because I’m a total noob at it, Derek has been nice enough to hold my hand throughout the dev process.
Django relies on the MVC design pattern, so you’ll want to modularize your components by creating Django apps. From the Django book:
if you’re using Django’s database layer (models), you must create a Django app. Models must live within apps. Thus, in order to start writing our models, we’ll need to create a new app.
The Django book is actually quite nice and useful, so if you’re new to Django, I recommend reading it. I still haven’t gotten through the whole thing myself, but I’ve got Derek
The steps to setup a Django app is actually quite long, so I made a nice little cheatsheet to help me remember the steps, so I’d though I’d share:
- Open up the console and
cdinto your project folder. To create the template files for your new app, you need to runpython manage.py startapp [your app's name] - Go into
setup.pyand add a new entry into theINSTALLED_APPSdictionary to include your new app, like this:'[Project Name].[App Name]', - [OPTIONAL] Inside your new app’s folder, create a subdirectory to hold all your
htmltemplates. By convention I called ittemplates - In
setup.pyupdate theTEMPLATE_DIRSdictionary to include yourhtmltemplates like this:CUR_DIR + '[App Name]/templates',whereCUR_DIR = os.getcwd() + os.setp() - If necessary, define the link to your application’s view in
urls.pyby modifyingurlpatterns
You’ll still need to create the html templates and define views for your application, but that’s basically how to start a new app in a nutshell.
Let me know if I missed out any steps (or if something’s just plain wrong:S)
Passing Parameters to a Function on HTML Events
Let’s say I have some function
foothat needs to be called when someHTMLelement is clicked:function foo(a, b) { : : : } : i = 100; j = 200; element.setAttribute("onclick", "foo(i, j)");In the example above, the Javascript interperter will see the line
element.setAttribute("onclick", "foo(i, j)");and automatically figure out that i and j should be 100 and 200, respectively.However, the same doesn’t work if you’re working with arrays indicies:
bar = new Array(); bar[0] = 100; bar[1] = 200; element.setAttribute("onclick", "foo(bar[0], bar[1])");To work around it, I had to re-assign them to variables:
bar = new Array(); bar[0] = 100; bar[1] = 200; i = bar[0]; j = bar[1]; element.setAttribute("onclick", "foo(i, j)");That definitely made me go…hunh??