gpt4 book ai didi

python - 无法获得正确的 url 来提供管理员上传的图像 django

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:03 27 4
gpt4 key购买 nike

我正在尝试获取一个模板来显示通过管理界面上传的图像,但我的运气为零。我尝试过查看有关该主题的现有问题,但没有一个有帮助。

下面是我的设置、模型、 View 和模板文件

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 工作。有人可以演示我应该如何获取要使用的模板的完整路径吗?

最佳答案

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

关于python - 无法获得正确的 url 来提供管理员上传的图像 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44657944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com