from django.db import modelsfrom PIL import Image
class Image(models.Model): imgfile = models.ImageField(upload_to='images/%Y/%m/%d') from django import forms
class ImageForm(forms.Form): imgfile = forms.ImageField( label='Select a file', )from django.shortcuts import render_to_responsefrom django.template import RequestContextfrom django.http import HttpResponseRedirectfrom django.core.urlresolvers import reverse
from .models import Imagefrom .forms import ImageForm
def list(request): # handling the file upload process if request.method == 'POST': form = ImageForm(request.POST, request.FILES) if form.is_valid(): newimg = Image(imgfile = request.FILES['imgfile']) newimg.save()
# point to the list of images return HttpResponseRedirect(reverse('img.views.list')) else: form = ImageForm() # show nothing
#load images on the list page images = Image.objects.all()
# render list page with the images & the browse form return render_to_response( 'img/list.html', {'images': images, 'form':form}, context_instance=RequestContext(request) )
{% extends "site_base.html" %}
{% load i18n %}
{% block head_title %}site for specific people{% endblock %}
{% block body_class %}list{% endblock %}
{% block body_base %} <section class="jumbotron"> <div class="container"> {% include "_messages.html" %} <h1>{% blocktrans %}<br>Add an image!{% endblocktrans %}</h1> <!-- List of uploaded documents --> {% if images %} {% for image in images %} {{ image.imgfile.title }} <img src="{{ image.imgfile.url }}" width="680" height="800" style="padding=20px; margin=20px" ></img>
{% endfor %} {% else %} <p>No images.</p> {% endif %}
</div>
<div class="container"> <div class="feature-columns"> <div> <form action="{% url 'list' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <p>{{ form.non_field_errors }}</p> <p>{{ form.imgfile.label_tag }} {{ form.image.help_text }}</p> <p> {{ form.imgfile.errors }} {{ form.imgfile }} <input type="submit" value="Upload" /></p> </div> </div> </div> </section>{% endblock %}{% extends "img/list.html" %}
{% load i18n %}
{% block head_title %}site for specific people{% endblock %}
{% block body_class %}home{% endblock %}
{% block body_base %} <section class="jumbotron"> <div class="container"> {% include "_messages.html" %} {% if not user.is_authenticated %} {% url "account_login" as login_url %} {% url "account_signup" as signup_url %} <p style="text-align: center;">{% blocktrans %}Feel free to <a href="{{ login_url }}" class="btn btn-default">Log In</a> or <a href="{{ signup_url }}" class="btn btn-primary">Sign Up</a> and post an image!{% endblocktrans %}</p> {% endif %} </div> </section> {% if images %} {% for image in images %} {{ image.imgfile.title }} <img src="{{ image.imgfile.url }}" width="680" height="800" style="padding=20px; margin=20px" ></img>
{% endfor %} {% else %} <p>No images.</p> {% endif %} </p> <div class="feature-columns" style="padding-bottom: 70%;"> </div> </div> </section> <section> <div class="container"> </p> </div> </section>{% endblock %}