The website documentation is so cool, more than that I found free book, you can get it here.
But what's Django and how it works?
As I said Django is a python Framework, written in pure python, then the matter is to know python and then Django will only accelerate your work
At its core, Django is simply a collection of libraries written in the Python programming language. To develop a site using Django, you write Python code that uses these libraries. Learning Django, then, is a matter of learning how to program in Python and understanding how the Django libraries work.
From the Django book
Ok, so we have to come back to Python, oh yes, here's a small comparision between python and django.
The example (found it in the django book) displays the ten most recently published books from a database.
The sample written in Pure Python
The sample written in Django
The example (found it in the django book) displays the ten most recently published books from a database.
The sample written in Pure Python
#!/usr/bin/python
import MySQLdb
print "Content-Type: text/html"
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"
connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")
for row in cursor.fetchall():
print "<li>%s</li>" % row[0]
print "</ul>"
print "</body></html>"
connection.close()
The sample written in Django
# models.py (the database tables)
from django.db import models
class Book(models.Model):
name = models.CharField(maxlength=50)
pub_date = models.DateField()
# views.py (the business logic)
from django.shortcuts import render_to_response
from models import Book
def latest_books(request):
book_list = Book.objects.order_by('-pub_date')[:10]
return render_to_response('latest_books.html', {'book_list': book_list})
# urls.py (the URL configuration)
from django.conf.urls.defaults import *
import views
urlpatterns = patterns('',
(r'latest/$', views.latest_books),
)
# latest_books.html (the template)
<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
</body></html>
I know that most of you will see that the django code is harder. Yes, it's longer and harder but smarter to use. I will prefer to use long code, then easily type book.name to display the latest books. The framework aims to make easy long and big application development.
No comments:
Post a Comment