我想为我的网站动态创建一个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函数,但事实证明这太难了。请帮忙