GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Django. Show all posts
Showing posts with label Django. Show all posts

Tuesday, May 10, 2011

GAE - Template

Introduction

Template makes your life easier. it separating between code logic of your program to what should display on the browser. Here's I just show up what the crucial template case that not handled by Django documentation.

Displaying List of Articles

It's really some often occurance for us to display item list. It's like blog post that consits of title, date, and content. Or it like personal data that consists of name, email, address and more. Here's for example we has some blog post:

Data Content

TitleContent
GAE TemplateGAE Template is use Django template system. So when you are accustomed for using Django, it's good for you.
GAE RuntimeThere are two available runtime for you. The first is Java and the second is Python. Whatever you choose, it should based on your preference for the language and your current skills.
GAE DatastoreIn GAE, your data is saved in database, but you never handle it directly, because Google Datastore that handles it, and in all your time, you handle the datastore. The operation for data in datastore is like in native sql, but it's more abstract than it that is called GQL - Google Query Language.
For example you want to display the data on the home page, what you need is you main the logic code that retrieves the data using Google datastore query.

Program code: main.py

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template

class Blog(db.Model):     title = db.StringProperty()
    content = db.TextProperty()

class MainPage(webapp.RequestHandler):
    blog = Blog()
    query = db.GqlQuery("SELECT * FROM Blog")
    data = []
    for row in query:
        mapp = {}
        mapp['title'] = row.title
        mapp['content'] = row.content
        data.append(mapp)


Share/Bookmark

Tuesday, April 19, 2011

Django - Login Logout

# views.py
def login(request):
    login = None
    if request.user.is_authenticated(): # check if user has login
        login = True
    else:
        login = False
    return render_to_response('polls/login.html', {'title': 'Login Logout Page', 'login':login})


def loginprocess(request):
    from django.contrib.auth import authenticate, login, logout
    from django.http import HttpResponseRedirect

    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)

    if user is not None:
        if user.is_active:
            login(request, user)  # login script
            return HttpResponseRedirect('http://localhost:8000/login')
        else:
            return render_to_response('polls/login.html', {'title': 'Disabled Account'})
    else:
        return render_to_response('polls/login.html', {'title': 'Not Registered Account'})
      

def logout(request):
    from django.contrib.auth import logout
    from django.http import HttpResponseRedirect

    logout(request)
    return HttpResponseRedirect('http://localhost:8000')

Share/Bookmark

Monday, April 18, 2011

Django - Redirection

This is a simple example of redirection mechanism in Django.
from django.http import HttpResponseRedirect
def my_view(request):
    return HttpResponseRedirect('http://google.com')
Share/Bookmark

Saturday, April 16, 2011

Django - Custom Response

HttpResponse is the basic for manipulating response to client.
For displaying an xml file, use
response = HttpResponse(mimetype="text/xml")
response.write('<?xml version="1.0"?>')
response.write('<data>No Data</data>')
return response

For displaying image, use
im = Image.open("nature.png")   # use PIL library
response = HttpResponse(mimetype="image/png")
im.save(response, "PNG")
return response
Share/Bookmark

Tuesday, April 5, 2011

Django - Admin Page

To make your application visible from admin panel, try to make admin.py in your app directory. But before do it, lets update some file:
1. urls.py -> uncomment line 4,5,15
2. settings.py -> uncomment line 94

from data.models import Personal
from django.contrib import admin

admin.site.register(Personal)

Remember that this use:
project = dj
app = data
site = Personal
 
Share/Bookmark

Django - Shell

from django.db import models

# Create your models here.

class Personal(models.Model):
    name = models.CharField(max_length=20)
    city = models.CharField(max_length=20)
    age = models.IntegerField()
    
    def __unicode__(self):
        return self.name

For example, you have models file like the above one, then on python manage.py shell (data is your project)
>>> from data.models import Personal
>>> p = Personal.objects.all()
>>> p = Personal(name='lady gaga', city='new york', age=24)
>>> p.save()     # flush the buffer
>>> p.name = 'Lady Gagah'   #updating
>>> p.save()
Share/Bookmark

Thursday, March 24, 2011

Django - Handling Data

>>> sys.path.append('C:\\Python27\\Project\\App')
>>> sys.path.append('C:\\Python27\\lucia\\blog')
>>> import your_app
>>> import blog

>>> User.object.all()

>>> u = User(username='lady gaga', password='secret')
>>> u.save()

>>> p.id
>>> p.username
>>> p.password

You can update a value in a table just by:
>>> p.username = 'aura kasih'
>>> p.save()

To get the table as object, use:
>>> a = User.objects.all()
>>> for i in a:
        print i.username, i.password
Share/Bookmark