请教朋友们,django upload file动态改变ImageField,FileField的upload

Static files管理
static files指一些用到的像css,javascript,images之类的文件。
在开发阶段:
1.在settings设置INSTALLED_APPS中添加'django.contrib.staticfiles'。
2.将STATIC_URL设置为'/static/'。
3.将某个app要用的静态文件放置到my_app/static/my_app中,例如my_app/static/my_app/my_image.jpg.
& 当然也可以直接放在my_app/static中,但这样的话,如果在不同的app中有相同名字的静态文件,就会产生冲突。
4.模板中使用
{% load staticfiles %}
&img src="{% static 'my_app/myexample.jpg' %}" alt="My image"/&
5.如果有些不是某个app特用的静态文件,可以建立static文件夹将静态文件放置其中,settings设置:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
在部署阶段,设置
STATIC_ROOT = "/var//static/"
python manage.py collectstatic
将各个app内的静态文件及STATICFILES_DIRS内的静态文件收集起来放置到STATIC_ROOT中由服务器apache或nhinx管理即可。
MEDIA:指用户上传的文件,比如在Model里面的FileFIeld,ImageField上传的文件。
假如有个Model
from django.db import models
class Car(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5, decimal_places=2)
photo = models.ImageField(upload_to='cars')
设置MEDIA_ROOT=os.path.join(BASE_DIR , 'media'),用来存储用户上传的文件
MEDIA_URL=/media/,为MEDIA_ROOT中的文件建立url地址。
当建立一个Car实例时,Car的ImageField字段图片就存到media/cars文件夹里面
&&& car = Car.objects.get(name="57 Chevy")
&&& car.photo
&ImageFieldFile: chevy.jpg&
&&& car.photo.name
u'cars/chevy.jpg'
&&& car.photo.path
u'/media/cars/chevy.jpg'
&&& car.photo.url
u'/media/cars/chevy.jpg'
在模板中使用图片
&img src="{{ car.photo.url }}" alt="My image"/&
在urls.py中使用&django.contrib.staticfiles.views.serve()&view
from django.conf import settings #from myapp import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这样就可以使用media文件了。
阅读(...) 评论()请教朋友们,django动态改变ImageField,FileField的upload_百度知道
请教朋友们,django动态改变ImageField,FileField的upload
save()其中obj是新建的或者修改后的对象,form是返回的表单(修改后的),写在admin.ModelAdmin),当新建一个对象时 change = False, change):
#处理你的图片
def save_model(self.py里面, obj, form.contrib import adminclass ArticleAdmin(admin,需要重写一个函数,或者在官网的文档上找from django, 你搜索 django 后台定制可以处理的, request
其他类似问题
为您推荐:
django的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁请教朋友们,django如何动态改变ImageField,FileField的upload_百度知道
请教朋友们,django如何动态改变ImageField,FileField的upload
提问者采纳
obj.save()其中obj是新建的或者修改后的对象, form:
#处理你的图片
obj.ModelAdmin),写在admin,form是返回的表单(修改后的).contrib import adminclass ArticleAdmin(admin.py里面, request:
def save_model(self,需要重写一个函数可以处理的, change), 你搜索 django 后台定制,当新建一个对象时 change = False,或者在官网的文档上找from django
来自团队:
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁For a while I’ve been using the
as a way to specify an upload path for images.
It’s a hack that lets you use ids or slugs from your models in the upload path, e.g.:
/path/to/media/photos/1234/flowers.jpg
/path/to/media/photos/scotland-trip/castle.jpg
CustomImageField no more
was merged in to trunk , it’s no longer necessary to use the custom field.
Other recent changes to trunk mean that it doesn’t work any more in its current state, so this is a good time to retire it.
Pass a callable in upload_to
It is now possible for the upload_to parameter of the
or ImageField to be a callable, instead of a string.
The callable is passed the current model instance and uploaded file name and must return a path.
That sounds ideal.
Here’s an example:
from django.db import models
def get_image_path(instance, filename):
return os.path.join('photos', str(instance.id), filename)
class Photo(models.Model):
image = models.ImageField(upload_to=get_image_path)
get_image_path is the callable (in this case a function).
It simply gets the id from the instance of Photo and uses that in the upload path.
Images will be uploaded to paths like:
photos/1/kitty.jpg
You can use whatever fields are in the instance (slugs, etc), or fields in related models.
For example, if Photo models are associated with an Album model, the upload path for a Photo could include the Album slug.
Note that if you are using the id, you need to make sure the model instance was saved before you upload the file.
Otherwise, the id hasn’t been set at that point and can’t be used.
For reference, here’s what the main part of the view might look like:
if request.method == 'POST':
form = PhotoForm(request.POST, request.FILES)
if form.is_valid():
photo = Photo.objects.create()
image_file = request.FILES['image']
photo.image.save(image_file.name, image_file)
This is much simpler than the hacks used in CustomImageField and provides a nice flexible way to specify file or image upload paths per-model instance.
Note: If you are using ModelForm, when you call form.save() it will save the file – no need to do it yourself as in the example above.
Update: Using the id of the instance doesn’t work any more because it’s probably not set when your function is called.
Try using a field such as a slug instead, or the id of a parent object (e.g. if the Photo is in an Album, use the Album‘s id.
Filed under:
— Scott @ 10:38 pm
Categories:django(21)
现在我有两个文件夹pro1和pro2,
pro1下面有0.mp4,1.mp4,pro2下面有0.mp4和1.mp4,我用django的
video = models.FileField(upload_to = 'myvideo')
虽然可以将视频都上传到myvideo目录下面,但是myvideo下面的文件是:
0. mp4,0_2.mp4,1.mp4,1_2.mp4了,因为出现一样的名字,django自动做了转换。
我希望能够直接在myvideo下面上传得到pro1和pro2两个文件夹,然后两个文件夹中
分别包含各自的0.mp4,1.mp4。
起初是参考了这篇文章:
然后我想到的方法,就是添加一个name字段用于作为视频文件的父目录,即用来填写pro1,pro2,。。。。
然后将upload_to固定的字符串改变为一个返回上传文件位置的函数,在这个函数中取获取之前的name字段,并添加到upload/下面这样就形成了动态目录
代码如下:
def get_photo_path(instance, filename):
productionName = instance.name
print productionName , & & , filename
return 'operatevideo/%s/%s' % (productionName , filename)
class Videos(models.Model):
name = models.CharField(unique=True,max_length = 255)
independent_video = models.FileField(upload_to = get_photo_path)
simulated_video = models.FileField(upload_to = get_photo_path ,validators)
class Meta:
db_table = 'videos'
大功告成,哈哈
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:109905次
积分:5366
积分:5366
排名:第3385名
原创:425篇
转载:80篇
评论:12条
(2)(18)(11)(1)(9)(1)(8)(3)(4)(23)(12)(268)(32)(5)(6)(13)(1)(4)(74)(9)(1)}

我要回帖

更多关于 django upload 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信