Tutorial :Django for a simple web application



Question:

I'm developing an app (an API) in python and I would like to offer some of its functionality through a web interface (like web services do). I've been looking at django, but I don't know if really fits well in my idea. I only want to create a web page that invokes to my API methods in order to acomplish the functionality that offers that web page. But, after followed the tutorial, I feel a little confused about the point of django. It seems to me to be more related with an ORM than a classic web application.

Is django a solution so heavy for such a simple development (as I mentioned, make calls to my API through the web)? Do I always have to use a database?

Thanks.


Solution:1

I love django but there is an lot of it to get your head around! If you don't want the database bit, focus on urls.py and views.py that will process your urls and return the info you want as an http response.

eg. urls.py

urlpatterns += patterns('myapp.views',        url(r'^getstuff/$', 'getstuff' ),  )  

in views.py

def getstuff(request):       do whatever in python       return HttpResponse(stuff to return)  


Solution:2

You don't need to use database in Django projects. Basically django comes with some standardized architecture that follows MVC pattern (or MVT as sometimes described). This includes models, views, url dispatching, templates, etc.

Probably you need to do following things to accomplish your task:

  1. create url definition in urls.py to some django view
  2. write django view that call somehow your api and displays result as a web page

you don't need models and database at all but you need to get familliar with views, urls, templates. It might look like a big machinery for your simple case but if you have some time I encourage you to these django basics.

If you are looking for somethin much simpler I heard about webpy project. This might be better option if you need something really simple.


Solution:3

An important question is: Do you want the web services to be provided by a full-featured server like Apache, or are you just looking at the "web server" to be a thread (or equivalent) in your program?

If you want to run Apache, then I'd recommend something like Werkzeug, which will handle most of the WSGI stuff for you. For templating, I've heard good things about Jinja2.

If that is too much, and all you want is a lightweight, simple server (something that, say, just spits out some HTML or XML when asked, and doesn't need any fancy URL handling), you can use the SimpleHTTPServer or CGIHTTPServer modules that ship with Python.

Django is a full-featured, integrated package that provides almost everything you need to write database-backed web applications. While its various components can be used in isolation, if you're only using one thing (the template and view engines, in your case), it is probably overkill.


Solution:4

No need for a framework at all. Raw wsgi isn't hard but a little verbose. So I like to use WebOb

Here's Raw wsgi

def application(environ, start_response):      start_response("200 OK", [])      return ["<html><body><h1>Hello World</h1></body></html>"]  

Here's the webob version

from webob.dec import wsgify  from webob import Request    @wsgify  def application(request):      return Response("<html><body><h1>Hello World</h1></body></html>")  

That's enough to run under apache mod_wsgi, and there are plenty of libraries that you can use that expect/produce webob Request and Responses. Anything that Turbogears 2 or repoze.bfg uses is fair game at that point.


Solution:5

You definitely don't have to use a database with Django. Whether it fits your needs, only you can tell. There are other Python web frameworks that you can use.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »