Mafinar Rashid Khan
Product Manager
Panacea Systems Limited
@mafinar
django-admin startproject myproject
django-admin startapp myapp
# In myproject/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', 'myapp.views.hello', name='hello'),
)
# In myapps/views.py
from django.http import HttpResponse
def hello(request):
# Remember those stamps I was talking about?
return HttpResponse("Hello World!!!")
# In myapp/templates/myapp/hello.html
<html>
<head><title>{{ title }}</title></head>
<body>
Hello, {{ name }}<br />
Countdown-
{% for i in countdown %}<p>{{ i }}</p>{% endfor %}
</body>
</html>
# In myapp/views.py
from django.shortcuts import render
def hello(request):
name = request.GET.get("name", "Anonymous")
return render("myapp/hello.html", {"name": name})
# In myapp/models.py
from django.db import models
class Category(models.Model):
name = models.CharField(unique=True)
description = models.TextField(blank=True,
null=True)
class Product(models.Model):
name = models.CharField(unique=True)
category = models.ForeignKey(Category)
def __unicode__(self):
return self.name
all_products = Product.objects.all()
sorted_products = Product.objects.all().order_by("-name")
categories = Category.objects.filter(name__istartswith="A")
category = Category.objects.create(name="Electronics",
description="Zap!")
category.name = "Electronics and Devices"
category.save()
category.delete()
Category.objects.raw("SELECT * FROM myapp_category")