0

I need help please. I want to upload my images but there's a problem A screenshot of what I get I uploaded my images dynamically, In the model I have:

....
class Visage(models.Model):
    personne = models.ForeignKey(Personne)

    def generate_filename(self, filename):
        return "personne/static/personne/Images/%s/%s"% (self.personne.nom,filename)

    image = models.ImageField(blank=False, upload_to=generate_filename)
    ...

In the template I have:

{% for image in visages_liste %}
        <a href="{{image.image.url }}"> <img src="{{image.image.url}}" height="420"></a>
    {% endfor %} 

the url of the image looks like: localhost:8000/personne/static/personne/Images/NameOfThePerson/NameOfTheImage.jpg

The view :

class IndexView (generic.ListView):
    template_name = 'personne/accueil.html'
    context_object_name = 'visages_liste'


    def get_queryset(self):
        return Visage.objects.all()

In the urls I've tried so many different things but no one worked. What do I should put in the STATIC_MEDIA and the STATIC_URL ? and what to put in the urls to work ?

Lilia
  • 462
  • 6
  • 22
  • When you say the url of the image is `/home/user/projet/personne/static/personne/Images/NameOfThePerson/NameOfTheImage.jpg`, is this the path where the image is stored, or is this the path that the browser is trying to render? – Curtis Olson Mar 19 '16 at 17:51
  • Both of them the path where the image is stored is : /home/user/projet/personne/static/personne/Images/NameOfThePerson/NameOfTheImag‌​e.jpg the link that the browser shows : http://localhost:8000/home/user/projet/personne/static/personne/Images/NameOfThePerson/NameOfTheImag‌​e.jpg – Lilia Mar 19 '16 at 17:56

2 Answers2

1

upload_to of ImageField needs to be the subroot after settings.MEDIA_ROOT.

Instead of

ImageField(blank=False, upload_to=os.path.join(MEDIA_ROOT, 'personee'))

please try

ImageField(blank=False, upload_to='personne')

then the image will be save in MEDIA_ROOT/personne.

EDIT: with your model setting,

Instead of

def generate_filename(self, filename):
    return os.path.abspath(os.path.join(MEDIA_ROOT, 'personne/static/personne/Images/'))+'/' + self.personne.nom+'/'+ filename

please try

def generate_filename(self):
    return 'personne/static/personne/Images/'+self.personne.nom

then the image will be uploaded in MEDIA_ROOT/personne/static/... as specified.

Leonard2
  • 894
  • 8
  • 21
  • The ImageField must be uploaded in a folder that has the name of the person. That's why I have: upload_to="..+self.personne.nom+'/'+ filename" – Lilia Mar 19 '16 at 17:30
  • 1
    @Lilia what I want to address is that `MEDIA_ROOT` is not needed for `upload_to`. how about the edited answer? – Leonard2 Mar 19 '16 at 17:50
  • At first, it was like this, but I had the same problems. – Lilia Mar 19 '16 at 17:50
  • I want to upload my images in a template – Lilia Mar 19 '16 at 17:52
  • 1
    I agree with @Leonard2; you can still generate the path, but Django will automatically append your path to the `MEDIA_ROOT`. So it might be wise to remove the `MEDIA_ROOT` from your `generate_filename` method. Otherwise, you risk having your images be located at `/path/to/media/path/to/media/personne/...`. Note the duplicate `/path/to/media/` – Curtis Olson Mar 19 '16 at 17:54
  • I edited my question now. The path that the browser shows is localhost:8000/personne/static/personne/Images/Person'sName/Image'sName.jpg – Lilia Mar 19 '16 at 18:16
  • @CurtisOlson I changed it and tried your answer. It solved my problem. – Lilia Mar 19 '16 at 18:37
  • Excellent, glad @Leonard2 and I could help – Curtis Olson Mar 19 '16 at 18:38
  • I am also glad it helped! And thanks for the help @Curtis :] – Leonard2 Mar 19 '16 at 18:41
0

In settings.py, try setting your MEDIA_ROOT to:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

And in your project's urls.py file add:

from django.conf import settings
from django.conf.urls import patterns

# Add this to the end of the urls.py file, after your other urls
if settings.DEBUG:
    urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}))
Curtis Olson
  • 947
  • 1
  • 5
  • 10
  • 1
    I believe @Leonard2's answer is correct for your case, as you shouldn't have `MEDIA_ROOT` in your `generate_filename` method. Update your method to match, and also make sure you have the `urls.py` snippet from my answer here. – Curtis Olson Mar 19 '16 at 18:04