For a little while
The team here have been looking at rapid development of web applications and have been trying python with Django to deliver some nice projects in a short period of time and with good results thus far. Up until the other day I really hadn’t done anything with Python and I had no intention to, but as these little skunk works projects have come to an end and the results look okay I figured it was worth learning a little.
Now, there’s always two approaches to this, start slow, learn python, learn some django and keep building on it until it all works and is fantastic or just dive in, I chose to dive in. The downside of diving in is you end up with some stupid errors where you don’t know the syntax, how the frame work works what should be done etc etc so you spend a lot of time googling! The advantage of this approach is you tend to learn more quicker, especially if you’re like me and struggle to read the doc and then apply, i’m only good with example sand then making mistakes, but with examples you’re on auto pilot.
So I had a bit of a chat today about the structure of Django where to put certain things and it was suggested to do something simple like a task list so that’s what I did, I made a simple task list, I decided to complicated it by using mongo as the database which the only example I had of it did not make use of the models in Django but tbh, I dived in i’m not sure what i was meant to to do there anyway… Although…. I do know that with MySQL I can use the models to outline the database structure and have the manage.py create the db for me, but alas I chose a harder route.
Some bad code
My view’s looks a little like this, I had started out with good intentions of using csfr but it was causing problems so I ended up removing it, in fact I removed quite a bit to simplify this to make it easier to debug, and it worked as this now works…
# Create your views here. from pymongo import Connection from django import forms from django.shortcuts import render_to_response from django.template import RequestContext from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse def add_task(request): return render_to_response('add.html') def add(request): if request.POST['task'] and request.POST['detail']: databaseName = "test" connection = Connection() db = connection[databaseName] todo = db['tasks'] task = {"task" : request.POST['task'], "detail" : request.POST['detail']} todo.save(task) return HttpResponseRedirect(reverse('task.views.list')) def list(response): databaseName = "test" connection = Connection() db = connection[databaseName] todo = db['tasks'] tasks = [] for task in todo.find(): tasks.append({ 'task': task['task'], 'detail': task['detail']}) #tasks.append(task['task']) print tasks return render_to_response('list.html', {"tasks":tasks})
The template for the add is very simple, it’s just a html form really not templating involved and it posts to the add method (which may have been the issue with the csfr stuff)
<html> <head>Test</head> <body> <p>Testing django</p> <h1>list</h1> <table align="centre"> <th> <td>Task</td><td>Details</td> </th> {% for task in tasks %} <tr> <td> {{ task.task }} </td> <td> {{ task.detail }} </td> </tr> {% endfor %} </table> </body> </html>
As you can see the template is straight forward, the bigest issue I had with the template is I was generating a json string so
tasks.append("{ 'task': '"+task['task']+"', 'detail': '"+task['detail']+"'}")
Although this looked okay it was really a problem, either way, so far from what I can tell Django is okay, python isn’t too bad and I will do some more over the coming weeks to help me understand it better but it seems okay, nothing to sing about just yet though.
[…] technology, not to any depth but just enough to get a feel for it. The week before last it was all Python & Django followed by AngularJS, well this week has been a bit more Javascript in the form of Node.js; […]