如何编写一个视图,响应函数传递的http请求?

时间:2013-08-09 08:36:08

标签: django django-views

urls.py

from django.conf.urls import patterns, url
from testform import views
urlpatterns = patterns('',
    url(r'^$', views.testhandler),
)

views.py

from django.http import HttpResponse
def testhandler(request):
    #return HttpResponse(request)

在views.py中如何返回传递testhandler的请求?

1 个答案:

答案 0 :(得分:0)

我不得不承认这是我在SO上见过的最奇怪的问题。无论如何,您可以获得视图返回request,如下所示:

from django.http import HttpResponse

def testhandler(request):
    return HttpResponse(request.__str__())

如果你想在浏览器中看到它更好一点,你可以尝试:

from django.http import HttpResponse

def testhandler(request):
    return HttpResponse('<br />'.join(request.__str__().split('\n'))) # convert \n to <br />