template not see img from Imagefiles

16 views
Skip to first unread message

Dariusz Mysior

unread,
May 13, 2016, 3:46:03 PM5/13/16
to Django users
I can't find where is a problem, I try show in template image and he don't show it. Please help...

my code

models

from django.db import models
from django.contrib.auth.models import AbstractUser

##################################################

class MysiteUser(AbstractUser):
avatar = models.ImageField(upload_to="avatar")

def __str__(self):
return self.username



template

<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% load staticfiles %}

{% block content %}
{% if user.is_authenticated %}
Mój profil {{ user.username }} </br>
<img src="{{ user.avatar.url }}" width="200" height="200" border="1" />
<a href="{% url 'users:logout-view' %}">Wyloguj się</a>
{% else %}
{% block logout %}
Nie jesteś zalogowany

{% endblock %}
{% endif %}
{% endblock %}
</body>
</html>


url

from django.conf.urls import  include, url
from django.contrib import admin
from users.views import LoginView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^users/', include('users.urls', namespace='users')),
url(r'^$', LoginView.as_view(), name='login-view'),


]

static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)


settings

MEDIA_ROOT = '/mysite/media/'
MEDIA_URL = '/media/'
STATIC_URL = '/static/'

Dariusz Mysior

unread,
May 13, 2016, 4:48:05 PM5/13/16
to Django users
Ok I have it

urls

url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),

settings

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
Reply all
Reply to author
Forward
0 new messages