Source

barcamp malacca / example / exampleapp / views.py

from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
# Create your views here.
from models import *

@login_required
def example_view(request):
    if request.method == 'POST':
        data = Example(name = request.POST.get('posting'),score = 0)
        data.save()
    res = Example.objects.all() 
    return render_to_response('example/template.html',{'res':res})
    
def example_register(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        email = request.POST.get('email')
        user = User.objects.create_user(username,email,password)
        user.save()
        return HttpResponseRedirect('/example/login/')
    else:
        return render_to_response('example/register.html',{})
        
def example_login(request):
    if request.method == 'POST': 
        username_ = request.POST.get('username')
        password_ = request.POST.get('password')
        user = authenticate(username = username_,password = password_)
        if user is not None:
            if user.is_active:
                login(request,user)
                return HttpResponseRedirect('/example/')
            else:
                return HttpResponseRedirect('/example/login/')
        else:
            return HttpResponseRedirect('/example/login/')
    else:
        return render_to_response('example/login.html',{})


def example_logout(request):
    logout(request)
    return HttpResponseRedirect('/example/login/')

@login_required
def example_like(request,id):
    data = Example.objects.get(id=id)
    data.score = data.score + 1
    data.save()
    return HttpResponseRedirect('/example/')

@login_required
def example_hate(request,id):
    data = Example.objects.get(id=id)
    data.score = data.score - 1
    data.save()
    return HttpResponseRedirect('/example/')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.