我在尝试删除上传的图片时遇到了问题.
错误是这样的:
SuspiciousOperation: Attempted access to '/media/artists/12-stones/154339.jpg' denied.
Run Code Online (Sandbox Code Playgroud)
看完后看起来错误是因为它正在寻找错误位置的图像(注意第一个斜杠,/ media /文件系统上不存在)
我的MEDIA_ROOT和MEDIA_URL是:
MEDIA_ROOT = '/home/tsoporan/site/media/'
MEDIA_URL = "/media/
Run Code Online (Sandbox Code Playgroud)
我的模型upload_to参数传递了这个函数:
def get_artist_path(instance, filename):
return os.path.join('artists', slugify(instance.name), filename)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
1)如何解决此问题以备将来上传?
2)是否可以修复当前图像的路径而无需重新上载?
此致,提图斯
这是在杀我!
我正在使用django-filebrowser,我想创建一个图库应用程序,利用它的上传功能来管理图像.
我有一个Gallery模型,允许用户在服务器上选择或创建目录,并将文件上传到该文件夹以显示在库中.我想自动搜索用户已将图像上传到的目录,然后选择该目录,然后自动为该文件夹中的每个图像创建图像实例.
class Gallery(model.Models):
gallerydirectory = FileBrowserField(...)
title = ...
description ...
class Image(model.Models):
image_field = models.ImageField()
Run Code Online (Sandbox Code Playgroud)
问题是FileBrowser表示图像与Django不同,但我想使用DJango ImageFields,因为我可以在模板端使用其他应用程序(sorl缩略图).
我有文件所需的所有数据,即文件名,路径等,我只是无法让Django创建一个ImageField的实例,而无需再次实际上传图像.我只是想填充它.
我在这里看到另一个提示如下的线程:
for image in filebrowser_image_objects_list:
f = File(open('path-to-file-on-server','r'))
i = Image()
i.image_field('filename.png',f.read())
Run Code Online (Sandbox Code Playgroud)
但这给了我一个:
SuspiciousOperation error
Attempted access to '/filename.png' denied
Run Code Online (Sandbox Code Playgroud)
这表明路径没有被正确读取.我打印了文件对象的属性,它正在打开正确的图像,它只是没有被传递到ImageField
帮助将非常感谢!
UPDATE
我已经放弃了尝试这项工作,因为它太乱了.我上面遇到的问题是我将ImageField的upload_field设置为'/'并且它没有被注意到意味着该文件被写入'/something.png'.
我已对其进行了修改,以便Image现在也使用FileBrowserField而不是ImageField.
我有一个具有ImageField的模型.如何手动为其分配图像文件?我希望它像任何其他上传文件一样对待它...
我一直在寻找一种从URL下载图像,在其上执行一些图像处理(调整大小)动作,然后将其保存到django ImageField的方法.使用两个很棒的帖子(下面链接),我已经能够下载图像并将其保存到ImageField.但是,一旦我拥有它,我一直在操作文件时遇到一些麻烦.
具体来说,模型字段save()方法需要File()对象作为第二个参数.所以我的数据最终必须是一个File()对象.下面链接的博客文章显示了如何使用urllib2将图像URL保存到File()对象中.这很好,但是,我还想使用PIL作为Image()对象来操作图像.(或ImageFile对象).
我首选的方法是将图像URL直接加载到Image()对象中,预先形成resize,然后将其转换为File()对象,然后将其保存在模型中.但是,我将Image()转换为File()的尝试失败了.如果可能的话,我想限制我写入磁盘的次数,所以我想在内存中使用这个对象转换或使用NamedTemporaryFile(delete = True)对象,所以我不必担心周围的额外文件.(当然,我希望通过模型保存文件后将文件写入磁盘).
import urllib2
from PIL import Image, ImageFile
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
inStream = urllib2.urlopen('http://www.google.com/intl/en_ALL/images/srpr/logo1w.png')
parser = ImageFile.Parser()
while True:
s = inStream.read(1024)
if not s:
break
parser.feed(s)
inImage = parser.close()
# convert to RGB to avoid error with png and tiffs
if inImage.mode != "RGB":
inImage = inImage.convert("RGB")
# resize could occur here
# START OF CODE THAT DOES NOT SEEM TO WORK
# I need to somehow convert an …Run Code Online (Sandbox Code Playgroud) 以下代码在保存后拍摄图像并从中制作缩略图:
class Image(models.Model):
image = models.ImageField(upload_to='images')
thumbnail = models.ImageField(upload_to='images/thumbnails', editable=False)
def save(self, *args, **kwargs):
super(Image, self).save(*args, **kwargs)
if self.image:
from PIL import Image as ImageObj
from cStringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile
try:
# thumbnail
THUMBNAIL_SIZE = (160, 160) # dimensions
image = ImageObj.open(self.image)
# Convert to RGB if necessary
if image.mode not in ('L', 'RGB'): image = image.convert('RGB')
# create a thumbnail + use antialiasing for a smoother thumbnail
image.thumbnail(THUMBNAIL_SIZE, ImageObj.ANTIALIAS)
# fetch image into memory
temp_handle = …Run Code Online (Sandbox Code Playgroud) 我需要一个用于在客户端裁剪图像的应用程序,我的意思是,使用像Jcrop jquery插件这样的裁剪工具.
我找到了这个工具:
但最后两个取决于管理员和两个首先似乎非常耦合自己的ImageFields和模型,任何好的解决方案?
我们正在研究一个具有许多功能的大型应用程序,并且很难改变逻辑写入
class Item(models.Model):
name = models.CharField(max_length = 200)
image = models.ImageField(upload_to = 'read', blank=True)
creative_url = models.CharField(max_length = 200)
description = RichTextField()
def save(self, *args, **kwargs):
content = urllib2.urlopen(self.creative_url).read()
self.image.save("test.jpg", File(content))
super(Item, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
给出异常:'str'对象没有属性'name'
我试图按照这个答案(http://stackoverflow.com/questions/1393202/django-add-image-in-an-imagefield-from-image-url)但它没有帮助摆脱异常.
AttributeError at /admin/collection/item/1/ 'str' object has no attribute 'name' Request Method: POST Request
URL: http://127.0.0.1:8000/admin/collection/item/1/ Django
Version: 1.2.5 Exception Type: AttributeError Exception Value: 'str'
object has no attribute 'name' Exception
Location: D:\FF\django\core\files\base.py in _get_size, line 39 Python
Executable: C:\Python27\python.exe Python Version: 2.7.2 Python
Path: ['D:\\FF', …Run Code Online (Sandbox Code Playgroud) 我有一个表单的userprofile
class profile():
#the next line is just an abstract
profile_images='volumes/media/root/userprofile/profile_images/'
image=models.ImageField(upload_to=profile_images)
Run Code Online (Sandbox Code Playgroud)
在"profile_images"目录中,用户上传的最后5个文件为个人资料图片,即:
image_1
image_2
image_3
image_4
image_5
Run Code Online (Sandbox Code Playgroud)
让我们说当前的profile.image是image_1.现在我想让用户选择以前的图像之一.我写的函数将图像更改为从表单中收到的图像看起来像:
def change_profile_image(userprofile,path_to_new_image):
f = open(path_to_new_image, 'r')
userprofile.image = ImageFile(f)
userprofile.save()
Run Code Online (Sandbox Code Playgroud)
作为示例,用户选择image_3,并且在执行该代码之后,前述目录看起来像:
image_1
image_2
image_3
image_4
image_5
volumes/media/root/userprofile/profile_images/image_3
Run Code Online (Sandbox Code Playgroud)
当然,这不是我想要的.我想要的只是更改与我的配置文件实例的ImageField相关联的文件,而不是Django复制任何文件.
任何想法如何解决?
我正在尝试从URL下载图像文件,然后将该图像分配给Django ImageField.我按照这里的例子和[这里](
我的模型在相关部分看起来像这样:
class Entity(models.Model):
logo = models.ImageField(upload_to=_getLogoPath,null=True)
Run Code Online (Sandbox Code Playgroud)
_getLogoPath回调非常简单:
def _getLogoPath(self,filename):
path = "logos/" + self.full_name
return path
Run Code Online (Sandbox Code Playgroud)
获取和保存图像文件的代码也很简单,作为我计划作为定期计划的cron作业运行的自定义django-admin命令的一部分:
...
img_url = "http://path.to.file/img.jpg"
img = urllib2.urlopen(img)
entity.logo.save(img_filename,img,True)
...
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到此错误:
AttributeError: addinfourl instance has no attribute 'chunks'
Run Code Online (Sandbox Code Playgroud)
我也尝试添加read()到图像但导致类似的错误.我也尝试将图像写入临时文件,然后尝试上传,但我得到了同样的错误.
我有一个使用easy_thumbnails(| thumbnail_url)显示的ImageField模型.
我的问题是如果ImageField为空,我如何显示默认图像?
我想在模型/视图中使用此逻辑,而不是在html /模板中.
例如:
DEFAULT_PICTURE = 'default.jpg'
def get_picture(self):
if self.picture:
return self.picture
else:
from DEFAULT_PICTURE
Run Code Online (Sandbox Code Playgroud)
get_picture()返回哪个对象与easy_thumbnails兼容?
我尝试创建一个新的File对象,就像这里一样,但它没有用.
您能否提供一个返回现有文件以使用easy_thumbnails显示的工作示例?
我的模板无法显示来自我的模型的图像。
src 属性带有正确的字段,但它不显示正确的图像
模板:
<div class="grid-product">
{% for p in prod %}
<div class=" product-grid">
<div class="content_box"><a href="single.html">
<div class="left-grid-view grid-view-left">
<img class="img-responsive watch-right" alt="not found" src="{{p.photo}}"/>
<div class="mask">
<div class="info">Quick View</div>
</div>
</a>
</div>
<h4><a href="#"></a></h4>
<p>It is a long established fact that a reader{{ p.p_name }}</p>
Rs. 499
</div>
</div>
{% endfor %}
<div id="show"></div>
<div class="clearfix"> </div>
</div>
Run Code Online (Sandbox Code Playgroud)
模型:
class product(models.Model):
p_id=models.AutoField(primary_key=True)
ps_id=models.ForeignKey(alphasubcat,on_delete=models.CASCADE)
p_name=models.CharField(max_length=100,null=True,blank=True)
photo = models.ImageField(upload_to='productimage',blank=True)
price=models.IntegerField()
def __str__(self):
return self.p_name
Run Code Online (Sandbox Code Playgroud)
看法:
def produc(request):
param=dict()
cat=categories.objects.all() …Run Code Online (Sandbox Code Playgroud) 我正在使用自定义保存功能将图像路径保存到我的模型图像字段。然后我意识到我只是在保存路径而不是实际对象。我的问题是如何使用 PIL 将该路径转换为对象。
django ×11
python ×7
urllib2 ×2
crop ×1
django-apps ×1
file ×1
imagefield ×1
jcrop ×1
jquery ×1
python-3.x ×1