Django 将动态创建的文件保存到 s3

发布于 2024-10-21 19:20:42 字数 1296 浏览 1 评论 0原文

我想为我的网站动态创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文