图像未加载 Django 网站
Images not loading Django website
我的 django 网站上的图片由于某种原因没有加载
这是我的models.py
thumbnail = models.ImageField(upload_to='images/',blank=True)
这是我的settings.py行
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/static-only')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/static'),
)
这是我的views.py
class BlogIndex(generic.ListView):
queryset = models.Entry.objects.published()
template_name = "index.html"
paginate_by = 5
在url.py
if settings.DEBUG:
urlpatterns +=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
在 index.html 我做了这个
<img class="img-rounded" src = "{{object.thumbnail.url}}"/>
点冻结
Django==1.11.2
django-ckeditor==5.2.2
django-crispy-forms==1.6.1
django-markdown==0.8.4
django-pagedown==0.1.3
Markdown==2.6.8
olefile==0.44
Pillow==4.1.1
图像被保存在 static/media/images 目录中
但是图片没有加载到网页中...
当我尝试右键单击并查看图像时显示此错误SCREEN SHOT
其他对象工作正常..只有图像没有加载
您需要 src 中的媒体 url:
<img class="img-rounded" src = "{{object.thumbnail.url}}"/>
应该是:
<img class="img-rounded" src = "{{MEDIA_URL}}{{object.thumbnail.url}}"/>
或类似的东西
<img class="img-rounded" src = "/static/media/uploads/{{object.thumbnail.url}}"/>
问题已解决,(得到 Facebook 群组的帮助)
问题是 views.py 中的 url 模式
我放的屏幕截图有一条线
提出者:blog.views.Blogdetail
显然我有一个 url 模式
url(r'^(?P<slug>\S+)/$', views.BlogDetail.as_view(), name='entry_detail'),
我的博客详细信息视图模式匹配 \S+ - 所以任何字符串中没有空格
这将匹配几乎所有以前未匹配的模式,并且将不会访问低于此特定 url 的任何其他 url 模式
将 url 模式更改为以下内容,我准备好了
url(r'^(?P<slug>[-\w]+)/$', views.BlogDetail.as_view(),name='entry_detail'),
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
在此模型中,您可以看到覆盖现有 IMG,return img 作为基本字符串:
models.py
from django.db import models
from django.core.files.storage import FileSystemStorage
from base64 import b64encode, b64decode
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=700):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name
def upload_location(instance, filename):
return os.path.join('defaultfolder', instance.phone, filename)
class ImgModel(models.Model):
name= models.CharField(max_length=100)
img = models.FileField(storage=OverwriteStorage(),
upload_to=phone_upload_location,
null=True,
blank=True,
max_length=700)
def image_to_base64(self, model_picture_path):
if model_picture_path:
try:
with open(model_picture_path.path, "rb") as imageFile:
str = b64encode(imageFile.read())
return "data:image/jpg;base64,%s" % str.decode()
except IOError:
return model_picture_path.url
else:
return None
.html
<img src="{{ MEDIA_URL }}foo.jpg">
我遇到了这个问题,我尝试了很多方法,但图像无法加载,然后我尝试了一个简单的技巧,但它成功了。我所做的是,在我的 settings.py 文件中,我在 BASE_DIR 之后包含了 STATIC_DIR 变量。下面是我的 settings.py 文件
Settings.py
#Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,"static")
#Other content
STATIC_URL = '/static/'
STATICFILES_DIRS=(
STATIC_DIR,
)
看看这个也许会有帮助!
如何修复图像答案是
上面提到index.html
{% static 'travello/images/' as baseUrl %}
路径
然后
我的 django 网站上的图片由于某种原因没有加载
这是我的models.py
thumbnail = models.ImageField(upload_to='images/',blank=True)
这是我的settings.py行
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/static-only')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/static'),
)
这是我的views.py
class BlogIndex(generic.ListView):
queryset = models.Entry.objects.published()
template_name = "index.html"
paginate_by = 5
在url.py
if settings.DEBUG:
urlpatterns +=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
在 index.html 我做了这个
<img class="img-rounded" src = "{{object.thumbnail.url}}"/>
点冻结
Django==1.11.2
django-ckeditor==5.2.2
django-crispy-forms==1.6.1
django-markdown==0.8.4
django-pagedown==0.1.3
Markdown==2.6.8
olefile==0.44
Pillow==4.1.1
图像被保存在 static/media/images 目录中 但是图片没有加载到网页中...
当我尝试右键单击并查看图像时显示此错误SCREEN SHOT
其他对象工作正常..只有图像没有加载
您需要 src 中的媒体 url:
<img class="img-rounded" src = "{{object.thumbnail.url}}"/>
应该是:
<img class="img-rounded" src = "{{MEDIA_URL}}{{object.thumbnail.url}}"/>
或类似的东西
<img class="img-rounded" src = "/static/media/uploads/{{object.thumbnail.url}}"/>
问题已解决,(得到 Facebook 群组的帮助)
问题是 views.py 中的 url 模式 我放的屏幕截图有一条线
提出者:blog.views.Blogdetail
显然我有一个 url 模式
url(r'^(?P<slug>\S+)/$', views.BlogDetail.as_view(), name='entry_detail'),
我的博客详细信息视图模式匹配 \S+ - 所以任何字符串中没有空格 这将匹配几乎所有以前未匹配的模式,并且将不会访问低于此特定 url 的任何其他 url 模式
将 url 模式更改为以下内容,我准备好了
url(r'^(?P<slug>[-\w]+)/$', views.BlogDetail.as_view(),name='entry_detail'),
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
在此模型中,您可以看到覆盖现有 IMG,return img 作为基本字符串:
models.py
from django.db import models
from django.core.files.storage import FileSystemStorage
from base64 import b64encode, b64decode
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=700):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name
def upload_location(instance, filename):
return os.path.join('defaultfolder', instance.phone, filename)
class ImgModel(models.Model):
name= models.CharField(max_length=100)
img = models.FileField(storage=OverwriteStorage(),
upload_to=phone_upload_location,
null=True,
blank=True,
max_length=700)
def image_to_base64(self, model_picture_path):
if model_picture_path:
try:
with open(model_picture_path.path, "rb") as imageFile:
str = b64encode(imageFile.read())
return "data:image/jpg;base64,%s" % str.decode()
except IOError:
return model_picture_path.url
else:
return None
.html
<img src="{{ MEDIA_URL }}foo.jpg">
我遇到了这个问题,我尝试了很多方法,但图像无法加载,然后我尝试了一个简单的技巧,但它成功了。我所做的是,在我的 settings.py 文件中,我在 BASE_DIR 之后包含了 STATIC_DIR 变量。下面是我的 settings.py 文件
Settings.py
#Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,"static")
#Other content
STATIC_URL = '/static/'
STATICFILES_DIRS=(
STATIC_DIR,
)
看看这个也许会有帮助!
如何修复图像答案是
上面提到index.html {% static 'travello/images/' as baseUrl %} 路径
然后