@kaflesudip
Fast
Batteries included
Secure
Community
We ♥ Python
Learn Python - http://www.diveintopython3.net/
Try Django's official Tutorial - https://docs.djangoproject.com/en/1.7/intro/tutorial01/
Learn Test Driven Development http://chimera.labs.oreilly.com/books/1234000000754
Learn about Django's best Practices http://www.amazon.com/Two-Scoops-Django-Best-Practices/dp/098146730X
Models -> Models
Views -> Templates
Controller -> Entire Framework
Browser
Views
Models
Templates
URLs
http://flipkarma.com/project/corporate-decision-support-system-for-telecommunic/
'^project/(?P<slug>[-\w]+)/$'
class ProjectDetailView(DetailView):
class Project(models.Model):
select * from Project where slug='...'
Raw Data
Data(objects)
Data + other..
HTML
Maps regular expressions to Python functions.
'/project/[0-9]+'
why not '^project/{id}/$'
Never do '/project/(.+)'
# product/models.py
class Product(models.Model):
name = models.Charfield()
description = models.StringField()
price = models.FloatField()
category = models.ForeignKeyField(subject)
def get_cheapest(self):
.........................
+---------------------+--------------+------
| Field | Type | Key |
+---------------------+--------------+-----|
| id | int(11) | PRI |
| name | varchar(300) | |
| description | longtext | |
| price | float | |
| category_id | int(11) | MUL |
+---------------------+--------------+-----+
Most of the business Logic reside in models
Gets the data from models and sends it to the templates
No business logic
from .models import Product
def product(request, id):
product_object = Product.objects.get(id=id)
return render_to_response("products.html", {"product": product_object})
HTML + some templating languages.
<!-- product.html -->
<!DOCTYPE html>
<html lang="en">
.............
<body>
<h1>{{product.name}}</h1>
{% if product.description %}
{{ product.description }}
{% endif %}
</body>
</html>
<!-- product.html -->
<!DOCTYPE html>
<html lang="en">
.............
<body>
<h1>Nike Shoes</h1>
The shoes are really awesome
priced at ...
</body>
</html>
# at models
def price_in_dollar(self, conversion_rate):
return (self.price) / conversion_rate
# at views
price_in_dollar = product.price_in_dollar(101)
# at templates
Price in $: {{price_in_dollar}}
Fat models
Thin views
Dumb templates
| | -- project
| | |-- app1
| | | |-- __init__.py
| | | |-- admin.py
| | | |-- forms.py
| | | |-- models.py
| | | |-- tests.py
| | | `-- urls.py
| | |-- app2
| | | |-- __init__.py
| | | |-- admin.py
| | | |-- forms.py
| | | |-- models.py
| | | `-- urls.py
| | |-- project
| | | |-- __init__.py
| | | |-- settings.py
| | | |-- urls.py
| | | `-- wsgi.py
| | |-- manage.py
| | |-- static
| | |-- templates
Design app that does one thing
One App for the complete project will make it painful later
Bad:
<a href="/product/{{id}}">......</a>
Good:
<a href="{% url 'product_page' id %}"....</a>
Prefer reverse in views.
reverse('product_page', args=(id,))
Use Virtualenv
Use Class Based Views
Django Debug Toolbar
iPython / bPython
API's - Django Rest Framework
Don't ignore tests.
Use Fabric, Ansible or chef for deployment
Master Python, data structures ...
Twitter / fb/ linkedin/ G+: @kaflesudip
meroanswer.com
Sudip Kafle
flipkarma.com
sudip@phunka.com
CTO and CoFounder at Phunka Technogies