Django for Designers: Difference between revisions

imported>Paulproteus
Line 2,122:
==== Handling user-uploaded media ====
 
So far, our website has only included visual effects created by the site admin. It's time to change that by letting your users upload custom images for tags.
(let's add an image to the Tag, and add a form for it)
 
A note about dependencies: For Django's image support to work properly, your system must have the Python Imaging Library. This is available either as "PIL", as "Image", or as "pillow".
 
Because that introduces a lot of complexity, this tutorial glosses over it. If you are especially excited about this, ask a TA to work with you on it.
 
Here are the basic steps:
 
First, you need to install PIL. Follow the instructions [https://developers.google.com/appengine/docs/python/images/installingPIL here] to do so.
 
Second, you need to modify your virtualenv to permit system site packages to work. To do that, do:
 
<source lang="bash">
$ virtualenv --system-site-packages .
</source>
 
Now you should be able to ''import Image'' in your python prompt. If not, talk to a TA.
 
We will need to enhance the tag model to accept an uploaded file. Open up ''bookmarks/model.py'' in your favorite editor. Change the Tag class definition so it looks like this:
 
<source lang="python">
class Tag(models.Model):
bookmark = models.ManyToManyField(Bookmark)
slug = models.CharField(max_length=50, unique=True)
image = models.ImageField()
</source>
 
You then need to ''create'' and ''execute'' a schema migration. Then, commit the changed models files ''and'' the the new migration files to git.
 
When that is done, you should add a new form to the tag.html view that lets users upload a new image. You will need to add a corresponding view that processes the form, and then redirects the user back to the tag view.
 
One final note: Because this section deals with file uploads, it will work inconsistently on Heroku. Heroku does not promise to keep any uploaded files. Your two options would be (1) reconfigure your app to use a different storage engine, for example uploading your images to a service like Amazon S3; or (2) you could use a different hosting service, such as OpenShift, that does not have this behavior.
 
==== Installing django-debug-toolbar and what it's useful for ====
Anonymous user