A Django view function must
Accept an HttpRequest instance as its first parameter
Returns a HttpResponse instance
The key to returning a non-HTML content from a view is that when constructing a HttpResponse class, you need to specify mimetype parameters. By changing the MIME type. We are able to tell the browser that the data to be returned is another type. Here's an example of a view that returns a PNG image. In order to make things as simple as possible. We simply read a picture stored on disk:
First put a picture into bidding\images\testpic.png, and then edit the view:
bidding\views.py:
def my_image(request):
image_data = open("Bidding/images/testPIC.png", "rb").read()
return HttpResponse(image_data, mimetype="image/png")
Change urls.py:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Bidding.views.home', name='home'),
# url(r'^Bidding/', include('Bidding.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', 'Bidding.views.hello'),
url(r'^time/$', 'Bidding.views.current_datetime'),
url(r'^time/plus/(\d{1,2})/$', 'Bidding.views.hours_ahead'),
url(r'^hello_base/$', 'Bidding.views.hello_base'),
url(r'^request_test/$', 'Bidding.views.request_test'),
url(r'^UsersSearch/$', 'Bidding.Users.views.search_form'),
url(r'^search/$', 'Bidding.Users.views.search'),
url(r'^ClassRoom/add/$', 'person.views.ClassroonAdd'),
url(r'^ClassRoom/list/$', 'person.views.ClassroonList'),
url(r'^ClassRoom/modify/(\d+)/$', 'person.views.ClassroonModify'),
url(r'^ClassRoom/delete/(\d+)/$', 'person.views.ClassroonDelete'),
url(r'^testPIC/$', 'Bidding.views.my_image'),
)
You can see this picture by opening http://127.0.0.1:8000/testPIC/ :
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagvtzw5nmtk4ma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
It's that simple. Assuming that changing the picture path in open () is a path to a real picture, you can use this very simple view to provide a picture. and the browser can display it correctly.
In addition, we must understand that the HttpResponse object implements the Python standard File Application interface (API). This means that you can use the "HttpResponse" instance in Python (or third party libraries) wherever the file is used.
Let's look at how to generate and output a PDF file with two Python plugins:
Reportlab-2.6.win32-py2.7.exe
Pil-1.1.7.win32-py2.7.exe
Double-click Install on-premises:
Join the View:
def hello_pdf(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=hello.pdf'
# Create the PDF object, using the response object as its "file."
p = canvas.Canvas(response)
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
p.drawString(100, 750, "Hello world.")
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
return response
Change urls.py:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Bidding.views.home', name='home'),
# url(r'^Bidding/', include('Bidding.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', 'Bidding.views.hello'),
url(r'^time/$', 'Bidding.views.current_datetime'),
url(r'^time/plus/(\d{1,2})/$', 'Bidding.views.hours_ahead'),
url(r'^hello_base/$', 'Bidding.views.hello_base'),
url(r'^request_test/$', 'Bidding.views.request_test'),
url(r'^UsersSearch/$', 'Bidding.Users.views.search_form'),
url(r'^search/$', 'Bidding.Users.views.search'),
url(r'^ClassRoom/add/$', 'person.views.ClassroonAdd'),
url(r'^ClassRoom/list/$', 'person.views.ClassroonList'),
url(r'^ClassRoom/modify/(\d+)/$', 'person.views.ClassroonModify'),
url(r'^ClassRoom/delete/(\d+)/$', 'person.views.ClassroonDelete'),
url(r'^testPIC/$', 'Bidding.views.my_image'),
url(r'^testPDF/$', 'Bidding.views.hello_pdf'),
)
you 're visiting testpdf.This page will be able to see the output of the PDF!
But now assuming that the code is uploaded, executing the program on the SAE will cause an error:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagvtzw5nmtk4ma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
This is because the SAE side does not have this third-party plugin installed. So let's explain how to install a third-party plugin on the SAE side:
Remember when we installed the local
Reportlab-2.6.win32-py2.7.exe
Pil-1.1.7.win32-py2.7.exe
, double-click after the fact is to copy the contents into a C:\Python27\Lib\site-packages called Reportlab, a call PIL. We compress the two packages into Reportlab.zip and pil.zip, and then we upload them to the web via SAE:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagvtzw5nmtk4ma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
You will see this result in the code editing page after completion:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagvtzw5nmtk4ma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Then change your INDEX.WSGI:
import sae
from Bidding import wsgi
import os
import sys
app_root = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(app_root, 'reportlab'))
application = sae.create_wsgi_app(wsgi.application)
Upload the server via SVN so that the SAE knows the third-party package you're using! In the implementation of the SAE end of the program. You will see the same situation as the local!
Some examples of output other files are also listed in the tutorial, which is not covered here, and we'll cover them in the following sections:
ZIP file :the Python Standard library includes a zipfile module. It is able to read and write compressed zip files. It can be used to generate compressed packages of files on demand, or to compress large documents when needed.
assumed to be TAR files can use the standard library tarfile module.
animated picture : Python picture processing library (pil; http://www.pythonware.com/products/ pil/) is an excellent image generation (PNG, JPEG, GIF It can be used to generate thumbnails for images and compress multiple images into separate frames. Or do a picture processing based on Web
Chart : Python has a number of great and powerful chart libraries for charting. On-demand maps. Forms and so on. We can't list all of them, so here's a list of the leaders.
§ matplotlib (http://matplotlib.sourceforge.net/) can be used to generate typically by matlab or A high-quality chart generated by Mathematica.
§ Pygraphviz (https://networkx.lanl.gov/wiki/pygraphviz) is a tool for Graphviz graphical layouts (http://graphviz.org/) Python interface that can be used to generate structured charts and networks.
The following tutorial has introduced the output of RSS, in fact, RSS is a very good thing, but the problem here is that the general content is the core of a site, provided that the RSS, users or other sites are able to subscribe to view without looking at advertising and other information, this is not the site webmaster is willing to see, so, This technology has not been a very good application at home. Interested students here can continue to see the http://djangobook.py3k.cn/2.0/chapter13/here Tutorial. It is not covered in this tutorial.
Python+django+sae Series Tutorial-----output non-HTML content (picture/pdf)