Tuesday, June 23, 2015

Re: Passing an image to the other url?

Jeremy, 

Extending a template do not magically gives you access to the context variables the other template has.
The context is set in the view, what's the view for the homepage? You didn't include it in your post.
Is the variable images set in there? I recommend using django debug toolbar to help debugging issues
like this. 

Anyway, make sure to set a context variable `images` in your homepage view and you will be all set.


On Monday, June 22, 2015 at 7:15:15 AM UTC-4, Jeremi Podlasek wrote:
Please beware that I'm just few weeks into django and really do not feel confortable at it

In my first django project I've created an app which is uploading images to the site, the images are stored at the same link the uploading takes place

What I'm trying to accomplish is to upload the images at the /list.html and to display the images on the homepage.
I've tried to inherit template from list.html so the homepage would have the variable images so I could iterate over it but to no avail.
I'm stuck and really a django framework beginner.

All the homepage says is "No images."



models.py 
from django.db import models
from PIL import Image

class Image(models.Model):
    imgfile = models.ImageField(upload_to='images/%Y/%m/%d')
    


forms.py
from django import forms

class ImageForm(forms.Form):
    imgfile = forms.ImageField(
        label='Select a file',
    )


views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from .models import Image
from .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)
    )


and templates: 

list.html
{% 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 %}

homepage.html
{% 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 %}



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1c3f80eb-81a4-44dc-9e95-9c1490d12919%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment