1

I don't have a clue here. I've set up a form with ImageField to upload user images via frontend and upload is working fine, though I can't find a way to retrieve the pics url and display it in a template. I did run python manage.py collectstatics. Template files and css load fine, but not any uploaded pictures. Any help is very welcome!

models.py

class Profil(models.Model):
    def content_file_name(instance, filename):
        return '/'.join(['pics', instance.user.username, filename])
    user_pic = models.ImageField(upload_to=content_file_name, blank=True, verbose_name='Foto')

template.html

<form class="mezzanine-form" action="" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend>Edit Profile</legend>
<img src="{{MEDIA_URL}}pics/{{user.username}}/{{WHAT_SHOULD_BE_HERE?}}">
    {% fields_for profile_form %}
    {% csrf_token %}
    <div class="form-actions">
        <input class="btn btn-primary btn-lg pull-right" type="submit" value="Save">
    </div>
    </fieldset>
</form>

settings.py

PROJECT_ROOT = "/path/to/project/"
PROJECT_DIRNAME = PROJECT_ROOT.split(os.sep)[-1]
CACHE_MIDDLEWARE_KEY_PREFIX = PROJECT_DIRNAME
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "static"),
    "/path/to/project/static/",
]
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))
ROOT_URLCONF = "%s.urls" % PROJECT_DIRNAME
TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, "templates"),)

httpd.conf

Alias /media/ /path/to/project/static/media/
Alias /static/ /path/to/project/static/

<Directory /path/to/project/static/>
Order allow,deny
Allow from all
</Directory>

<Directory /path/to/project/static/media>
Order allow,deny
Allow from all
</Directory>

1 Answers1

0

You can use the .url() method of ImageField. I assume that you have a ForeignKey to User in your Profil model, so the template code should be:

<img src="{{ user.profil.user_pic.url }}">
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Apache config can't affect this. The problem lies in your settings. What exactly do you see when you view source in your browser? – Selcuk Mar 12 '15 at 02:08
  • with in template and upload_to in model still same, the source in browser is – Jörg Maier-Rothe Mar 12 '15 at 08:25
  • This is the expected URL per your `MEDIA_URL = STATIC_URL + "media/"` setting. Your `MEDIA_ROOT` might be evaluating to a wrong value. Try setting it directly like `MEDIA_ROOT='/path/to/project/static/media'` to see if it helps. – Selcuk Mar 12 '15 at 09:26
  • 1
    That was the setting that needed change! You're ace! I'm confused though, upload was saved in "PROJECT_ROOT/static/media/pics/" and the browser was looking for the file in "PROJECT_ROOT/my_project/static/media/pics/" setting MEDIA_ROOT to "/path/to/PROJECT_ROOT/my_project/static/media" did the job! I propably mixed something up when running collectstatics Thank you so much! – Jörg Maier-Rothe Mar 12 '15 at 10:21