1、直接输出 -- HTTPResponse
helloworld.py
from django.utils.httpwrappers import HttpResponse
def index(request):
return HTTPResponse('''<html><head></head><body>Hello world</body></html>''');
def out(request):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=1.txt'
response.write('abcdef')
return response
urls.py
urlpatterns = patterns('',
(r'^$', 'test.helloworld.index'),
)
from django.utils.httpwrappers import HttpResponse
def index(request):
return HTTPResponse('''<html><head></head><body>Hello world</body></html>''');
def out(request):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=1.txt'
response.write('abcdef')
return response
urls.py
urlpatterns = patterns('',
(r'^$', 'test.helloworld.index'),
)
2. MTC -- render_to_response
helloworld.py (view)
#coding=utf-8
from django.core.extensions import render_to_response
def index(request):
return render_to_response('helloworld', {'params': {'a':1, 'b':2} } )
from django.core.extensions import render_to_response
def index(request):
return render_to_response('helloworld', {'params': {'a':1, 'b':2} } )
helloworld.html (template)
<html>
<body>
<table>
{% for key in params%}
<tr><td> {{key}} </td><td> {{params.key}} </td></tr>
{% endfor %}
</table>
</body>
</html>
<body>
<table>
{% for key in params%}
<tr><td> {{key}} </td><td> {{params.key}} </td></tr>
{% endfor %}
</table>
</body>
</html>
以上是One Piece总结的,下面我再补充一个,其实这个是第一种的变样
3、从template加载
from django.template import loader, Context
t = loader.get_template('common/post_note.htx')
c = Context({'action': request.path , 'title': consts.ADD_TOPIC})
return HttpResponse(t.render(c))
t = loader.get_template('common/post_note.htx')
c = Context({'action': request.path , 'title': consts.ADD_TOPIC})
return HttpResponse(t.render(c))