Aleksander Monk
Aleksander Monk

Reputation: 2907

Django STATIC_URL is empty, images doesn't show

I know that there is plenty of questions like that, I read them all and tried everything, however nothing solved my problem.

I'm writing something like blog in Django and of course I want to show pictures to users. Pictures are stored at /static/users/<username>/<image>.

My setting.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.static'
            ],
        },
    },
]



WSGI_APPLICATION = 'Travel.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_URL = '/users/'

What I have in html

 {% load static from staticfiles %}
 <img src="{% static comment.author.picture.url %}"
                                         width="70px" height="70px">

and what I have in views

@login_required
def trip_one(request, pk):
    trip = get_object_or_404(Trip, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = Person.objects.get_by_natural_key(request.user.get_username())
            comment = Comment(text=cd['text'],
                              author=user,
                              trip=trip)
            comment.trip = trip
            comment.save()
    comments = trip.comment_set.all()
    form = CommentForm()
    return render(request, 'TravelBuddy/travel.html', {'trips': trip,
                                                       'form': form,
                                                       'comments': comments})

And my link to images in browser looks like this

http://127.0.0.1:8000/users/default/default.png

When they have to look like this

http://127.0.0.1:8000/static/users/default/default.png

I cant just add static to url in html because it doesn't work on other pages.

So, what I am missing? Or what is wrong here?

Upvotes: 0

Views: 325

Answers (2)

Patrick Beeson
Patrick Beeson

Reputation: 1695

It appears that you need to refer to MEDIA instead of STATIC in this case.

Take a look at your MEDIA_ROOT and MEDIA_URL in settings to ensure they're pointed to the right place.

Then, in your template, you can get the URL for the object via the attribute:

<img src="{{ comment.author.picture.url }}" width="70px" height="70px">

More on MEDIA in the docs: https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-MEDIA_ROOT

Upvotes: 3

marcusshep
marcusshep

Reputation: 1964

Try changing MEDIA_URL to '/static/users/'

Upvotes: 1

Related Questions