我想使用'html'来显示这个视图...但是它给出了“/ error /的TypeError”

时间:2013-12-20 06:07:30

标签: python django

我是Django编码的新手......我想在area_distnce.html中显示任何内容......下面是我的代码

提前致谢....

Distance.py

from  django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext


def area_distance(request):    
    return render_to_response('area_distance.html',locals(),context_instance=RequestContext(request))

urls.py

from django.conf.urls import patterns, include, url
from areas.Distance import area_distance


urlpatterns = patterns('',       
url(r'^area/$','area_distance',name = 'area_distance'),)

当我跑他的时候就是给予     / area /的TypeError     'str'对象不可调用

2 个答案:

答案 0 :(得分:0)

您似乎在area_distance()而不是Distance.py中编写了视图功能views.py

因此,您可能希望更新urls.py以正确使用

url(r'^area/$', area_distance,name = 'area_distance'),)
---------------^ note no '...'

正如您所做的from areas.Distance import area_distance这应该有用。

答案 1 :(得分:0)

from  django.http import HttpResponse
from django.template.context import RequestContext

def area_distance(request):   
    a= 1
    b = a+1

    t = get_template('area_distance.html')
    c = RequestContext(request, locals())
    return HttpResponse(t.render(c))

#url.py
from django.conf.urls import patterns, include, url
from areas.Distance import area_distance
urlpatterns = patterns('',       
url(r'^area/$',area_distance,name = 'area_distance'),)

#template
{{ a }}
{{ b }}