在Django中从urls.py输出文本 [英] Outputing text from urls.py in Django
本文介绍了在Django中从urls.py输出文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以简单地从urls.py中输出文本(不是HTML),而不是调用函数(在views.py或其他方式中)吗?
Is there any way I can simply output text (not HTML) from urls.py instead of calling a function (in views.py or otherwise)?
urlpatterns = patterns('',
url(r'^$', "Hello World"),
)
推荐答案
不,一定要。 url
必须将模式映射到可调用对象,并且该可调用对象必须接受 HttpRequest
作为第一个参数,并返回 HttpResponse
。
No, definitly. A url
must map a pattern to a callable, and this callable must accept a HttpRequest
as first argument and return a HttpResponse
.
您可以做的更接近的事情是将模式映射到lambda(匿名函数),即:
The closer thing you could do would be to map the pattern to a lambda (anonymous function), ie:
from django.http import HttpResponse
urlpatterns = patterns('',
url(r'^$', lambda request: HttpResponse("Hello World", content_type="text/plain")),
)
但我肯定不会将此视为一种好习惯。
but I would definitly not consider this as good practice.
这篇关于在Django中从urls.py输出文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文