无法将第二个模板加载到My Django项目中

时间:2015-07-09 08:00:44

标签: python django

我是Django的初学者,需要一点帮助

我的应用程序的布局如下

adray/
    __init__.py
    admin.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py 
    static
    Templates/
       adray/
         index.html
         happen.html

我正在尝试将两个html文件加载到两个不同的方法中:index&请求,虽然索引工作,请求没有,似乎loader.get_template方法第二次不起作用,我得到以下异常:

'utf-8' codec can't decode byte 0x85 in position 3269: invalid start byte

Views.py:

from django.shortcuts import render
#from adray.models import Item 
from django.http import HttpResponse
from django.template import RequestContext, loader,Template 

def index(request):
        template = loader.get_template('adray/index.html')
        return HttpResponse(template.render())

    def happen(request):
        template = loader.get_template('adray/happen.html') # this second load doesn't work!
        return HttpResponse(template.render())

当我将发生的方法改为:

def happen(request):
           # template = loader.get_template('adray/happen.html') 
            return HttpResponse(request)

它工作正常(happen.html确实存在,检查过!)

urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from adray import views

urlpatterns = [
   url(r'^$', views.index),
   url(r'^happen/$', views.happen),
]

1 个答案:

答案 0 :(得分:1)

可能是您的模板文件编码错误。请查看此页面:http://blog.p3infotech.in/2013/fixing-template-unicodedecodeerror-in-django/