Django 将动态创建的文件保存到 s3
我想为我的网站动态创建一个 css 文件,当我上传新的导航项目时该文件会更新。我知道我需要覆盖导航模型的保存,并且在文件上传到我的 s3 后,我可以调用一个函数来执行某些操作,但我不知道如何动态创建文件然后上传它到同一个 s3 服务器,但位置不同。
from django.db import models
from django.forms import CheckboxSelectMultiple
import tempfile
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage as s3_storage
from django.core.cache import cache
from datetime import datetime
import Image, os
import PIL.Image as PIL
import re, os, sys, urlparse
def createDynamicCSS():
#create then save a css file here
#probably best to 'navigation/nav.css'
#loop through all entries and create class for all of them
navs = PrimaryNav.all()
cssfile = ""
for n in navs:
string = "." + n.slug + " { ... }"
cssfile += string
#save cssfile to s3
class PrimaryNav(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200)
active = models.BooleanField()
#saves to s3 (s3 address + /icon/ )
icon = models.ImageField(upload_to='icons')
def save(self):
super(PrimaryNav, self).save() # Call the "real" save() method
createDynamicCSS()
...
我不知道从哪里开始。我本来打算尝试剖析我在网上看到的 csv 函数,但事实证明这太困难了。请帮忙
I am wanting to dynamically create a css file for my site that updates when I upload new navigation items. I know that I need to override the save of my navigation model and after the file has been uploaded to my s3 I can then call a function to do some action, but I don;t know how I can create a file dynamically and then upload it to the same s3 server, but a different location.
from django.db import models
from django.forms import CheckboxSelectMultiple
import tempfile
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage as s3_storage
from django.core.cache import cache
from datetime import datetime
import Image, os
import PIL.Image as PIL
import re, os, sys, urlparse
def createDynamicCSS():
#create then save a css file here
#probably best to 'navigation/nav.css'
#loop through all entries and create class for all of them
navs = PrimaryNav.all()
cssfile = ""
for n in navs:
string = "." + n.slug + " { ... }"
cssfile += string
#save cssfile to s3
class PrimaryNav(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200)
active = models.BooleanField()
#saves to s3 (s3 address + /icon/ )
icon = models.ImageField(upload_to='icons')
def save(self):
super(PrimaryNav, self).save() # Call the "real" save() method
createDynamicCSS()
...
I'm not sure where to start. I was going to try and dissect the csv functions I have seen around the web, but that proved to be too difficult. Help please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论