Demo entry 5408442

Pyhton higligth

   

Submitted by anonymous on Jun 23, 2016 at 15:27
Language: Python 3. Code size: 1.7 kB.

from django.shortcuts import render, redirect, HttpResponseRedirect
from django.template.loader import get_template
from django.http import Http404, HttpResponse
from .models import Article
from django.contrib import auth
from django.contrib.auth.models import User
from django.contrib.auth import authenticate ,login  ,logout
from django.contrib.auth.decorators import login_required
from django import forms
# Create your views here.


def article(r):
    if r.user.is_active:
        return redirect(article_query, 1)
    else:
        a = Article.objects.all()
        return render(r, 'article/src.html', {'a': a})


def log_out(r):
    auth.logout(r)
    return HttpResponseRedirect('/article/')


def signin(request):
    a = Article.objects.all()
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    if request.method == 'POST':
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect(article_query, 2)
        else:
            return render(request, 'article/src.html', {'a': a})
    else:
        return render(request, 'article/src.html', {'a': a})


@login_required(login_url='article')
def article_query(r, i):
    name = r.user.username
    if r.user.is_authenticated():
        try:
            a = Article.objects.get(pk=i)
        except Article.DoesNotExist:
            raise Http404("Article Doesn't exist.")
        return render(r, 'article/query.html', {'a': a, 'n': name})
    return HttpResponse('You are not logged in.')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).