无法获得正确的网址来管理管理员上传的图片django

时间:2017-06-20 16:03:17

标签: python django django-templates django-admin

我正在尝试获取一个模板来显示通过管理界面上传的图像,而我却没有运气。我已经尝试查看有关此主题的预先存在的问题,但没有人帮助过。

以下是我的设置,模型,视图和模板文件

settings.py

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






DEBUG = True

ALLOWED_HOSTS = []
PROJECT_DIR_PATH = path.dirname(path.normpath(path.abspath(__file__)))




INSTALLED_APPS = [
    'about.apps.AboutConfig',
    'forms.apps.FormsConfig',
    'resources.apps.ResourcesConfig',
    'contact.apps.ContactConfig',
    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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 = 'nydkcd11.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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.template.context_processors.media',
                'django.template.context_processors.static',
            ],
        },
    },
]

WSGI_APPLICATION = 'nydkcd11.wsgi.application'




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




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



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True



MEDIA_ROOT = 'media/'
MEDIA_URL = '/media/'


STATIC_URL = '/static/'

models.py

from django.db import models
class Division(models.Model):
    name = models.CharField(max_length = 100)
    school = models.CharField(max_length = 75)
    position = models.CharField(max_length = 50)
    desc = models.TextField()
    image = models.ImageField(upload_to='images/')
    def __str__(self):
        return self.name

views.py

def division(request):
    member_list = Division.objects.all()
    return render(request, 'about/division.html',{'member_list':member_list})
    return HttpResponse("this is the divison page")

division.html

<h1>Divisional Board Members</h1>
{% for member in member_list%}
    <h2>{{member.name}}</h2>
    <h3>{{member.school}}</h3>
    <h3>{{member.position}}</h3>
    <img src = "{{member.image.url}}">
    <p>{{member.desc}}</p>
    <p>{{MEDIA.ROOT}}</p>
    <p>{{MEDIA.URL}}</p>
{% endfor %}

出现的具体问题是未生成完整目录。例如,我将获得的网址为media/images/example.jpg,但不包括完整的绝对路径。这(毫不奇怪)阻止http://127.0.0.1:8000/media/images/example.jpg工作。有人可以证明我应该如何获得模板的完整路径?

1 个答案:

答案 0 :(得分:1)

from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

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

将此内容放入您的urls.py