我无法显示数据库中保存的图像

时间:2019-02-03 16:28:02

标签: django django-forms

我无法显示保存在数据库中的图像,所有其他数据都出现了,唯一的问题是图像,在html中,我尝试加载图像的3个命令员。 我是python和django的初学者,非常欢迎您的帮助 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

models.py:

class Usuario(models.Model):
     foto = models.ImageField( blank=False, verbose_name="Foto para seu perfil", upload_to='sistema/img')

settings.py:

    import os

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '$n+6cd+9w+44=z*0o=8b#t&9*i!_ay%&6+kl=_cbq0%*sm0)c('

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = ['.127.0.0.1',]



    # Application definition

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'sistema',
        'PetAqui',
        'bootstrapform',
        'widget_tweaks',
        'crispy_forms',
        'multiselectfield',
    'django.contrib.sites',
        'rest_framework',
        'rest_framework.authtoken',
        'rest_auth',
        'rest_auth.registration',
        'allauth',
        'allauth.account',
        'allauth.socialaccount',


    ]

    SITE_ID=1

    CRISPY_TEMPLATE_PACK = 'bootstrap4'
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',

    ]

    ROOT_URLCONF = 'PetAqui.urls'

    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',
                ],
            },
        },

    ]

    WSGI_APPLICATION = 'PetAqui.wsgi.application'


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

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'petaqui',
            'USER': 'postgres',
            'PASSWORD': 'gt1utff7st3re',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }


    # Password validation
    # https://docs.djangoproject.com/en/2.1/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/2.1/topics/i18n/

    LANGUAGE_CODE = 'pt-br'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


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

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')



    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),

    )



    EMAIL_USE_TLS = True
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_HOST_USER = 'estacioneaqui24@gmail.com'
    EMAIL_HOST_PASSWORD = '10038561003856'
    EMAIL_PORT = 587


    LOGIN_REDIRECT_URL = 'sistema_index'
    LOGOUT_REDIRECT_URL = 'sistema_index'
    LOGIN_URL = 'sistema_index'

    MEDIA_ROOT='/sistema/img/'
    MEDIA_URL='/img/'

html:

    <img src="{{MEDIA_URL}}/{{ user.usuario.foto }}" >
        {{ user.usuario.foto  }}
        {{ user.usuario.foto.url  }}

urls.py

    from django.conf.urls import url
    from django.urls import include, path
    from . import views

    from .views import (
        index,
        cadastro,
        cadastro_novo,
        cadastro_negocio,
        activate,
        account_activation_sent,
        perfil

    )

    urlpatterns = [
        url(r'^index/$', index, name='sistema_index'),
        url(r'^cadastro/$', cadastro, name='sistema_cadastro'),
        url(r'perfil/$', perfil, name='sistema_perfil'),
        url(r'^cadastro-novo/$', cadastro_novo, name='sistema_cadastro_novo'),
        url(r'^cadastro-negocio/$', cadastro_negocio, name='sistema_cadastro_negocio'),
        url(r'^account_activation_sent/$', views.account_activation_sent, name='account_activation_sent'),
        path('activate/<uidb64>/<token>/', views.activate, name='activate'),


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

1 个答案:

答案 0 :(得分:1)

尝试使用

<img src="{{ user.usuario.foto.url }}" />

如果这不起作用,则包括urls.py文件,要浏览的URL以及返回的响应。