Django-Web小技巧 get_template 使用

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from django.template.loader import  get_template
from datetime import datetime
from .models import Post
# Create your views here.

def homepage(request):
    template = get_template('index.html')
    html = template.render(locals())
    return HttpResponse(html)

def index(request):
    template = get_template('index.html')
    posts = Post.objects.all()
    now = datetime.now()
    html = template.render(locals())
    return HttpResponse(html)

def home(request):
    template = get_template('home.html')
    posts = Post.objects.all()
    now = datetime.now()
    html = template.render(locals())
    return HttpResponse(html)

def showpost(request,slug):
    template = get_template('post.html')
    try:
        post = Post.objects.get(slug = slug)
        if post != None:
            html = template.render(locals())
            return HttpResponse(html)
    except:
        return redirect('/')

def showdetail(request,slug):
    template = get_template('show.html')
    try:
        post = Post.objects.get(slug = slug)
        if post != None:
            html = template.render(locals())
            return HttpResponse(html)
    except:
        return redirect('/')

你可能感兴趣的:(Django相关)