Ich weiß, dass es viele Fragen wie diese gibt, ich las sie alle und versuchte alles, aber nichts löste mein Problem.Django STATIC_URL ist leer, Bilder zeigen nicht
Ich schreibe etwas wie Blog in Django und natürlich möchte ich den Benutzern Bilder zeigen. Bilder werden unter /static/users/<username>/<image>
gespeichert.
Mein 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/'
Was habe ich in html
{% load static from staticfiles %}
<img src="{% static comment.author.picture.url %}"
width="70px" height="70px">
und was habe ich in Aussicht
@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})
Und mein Link auf Bilder in Browser wie folgt aussieht
http://127.0.0.1:8000/users/default/default.png
Wenn sie wie diese
http://127.0.0.1:8000/static/users/default/default.png
suchen kippe ich nur hinzufügen, static
in html url, weil es nicht auf anderen Seiten funktioniert.
Also, was ich vermisse? Oder was ist hier falsch?
die Bilder sind Sie wollen Zeige tatsächlich Medien (was bedeutet, dass sie in der Datenbank verwaltet werden)? –
Ja, sie sind @ PatrickBeeson –