我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.conf.settings.STATIC_ROOT。
def __init__(self, app_names=None, *args, **kwargs): # List of locations with static files self.locations = [] # Maps dir paths to an appropriate storage instance self.storages = OrderedDict() if not isinstance(settings.STATICFILES_DIRS, (list, tuple)): raise ImproperlyConfigured( "Your STATICFILES_DIRS setting is not a tuple or list; " "perhaps you forgot a trailing comma?") for root in settings.STATICFILES_DIRS: if isinstance(root, (list, tuple)): prefix, root = root else: prefix = '' if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root): raise ImproperlyConfigured( "The STATICFILES_DIRS setting should " "not contain the STATIC_ROOT setting") if (prefix, root) not in self.locations: self.locations.append((prefix, root)) for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super(FileSystemFinder, self).__init__(*args, **kwargs)
def check_settings(base_url=None): """ Checks if the staticfiles settings have sane values. """ if base_url is None: base_url = settings.STATIC_URL if not base_url: raise ImproperlyConfigured( "You're using the staticfiles app " "without having set the required STATIC_URL setting.") if settings.MEDIA_URL == base_url: raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL " "settings must have different values") if ((settings.MEDIA_ROOT and settings.STATIC_ROOT) and (settings.MEDIA_ROOT == settings.STATIC_ROOT)): raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT " "settings must have different values")
def test_munge_config(self): def mock_find(path): return os.path.join('/some/path/static/', path) with mock.patch('webpack.conf.find', new=mock_find) as find: munged = get_munged_config(WEBPACK_CONFIG) expected_output = WEBPACK_CONFIG_OUTPUT.format( url=settings.STATIC_URL, root=settings.STATIC_ROOT ) self.assertEqual( munged, expected_output )
def file_hash(filename): if not filename: return None def _get_full_path(path): rel_source_path = path.lstrip("/") if settings.STATIC_ROOT: full_path = os.path.join(settings.STATIC_ROOT, rel_source_path) if os.path.exists(full_path): return full_path try: full_path = finders.find(rel_source_path) except Exception: full_path = None return full_path full_path = _get_full_path(filename) return None if full_path is None \ else hashlib.md5(open(full_path, 'rb').read()).hexdigest()[:7]
def heatmap_to_twitter(): try: now = date.today() d = now.day if d == 2: api = connect() for broker in Brokers.objects.all(): image_filename = join(settings.STATIC_ROOT, 'collector', 'images', \ 'heatmap', '{0}=={1}=={2}=={3}=={4}.png'.format(broker.slug, \ 'AI50', '1440', 'AI50', 'longs')) if isfile(image_filename): media = "https://quantrade.co.uk/static/collector/images/heatmap/{0}=={1}=={2}=={3}=={4}.png".\ format(broker.slug, 'AI50', '1440', 'AI50', 'longs') else: media = None status = "Results including last month index performance for {}.".format(broker.title) api.PostUpdate(status=status, media=media) print(colored.green("Heatmap posted.")) except Exception as e: print(colored.red("At heatmap_to_twitter {}".format(e)))
def get_project(**kwargs): project = OrderedDict() project["current_dir"] = os.path.realpath(os.curdir) project["tempdir"] = tempfile.gettempdir() if config.MEDIA_ROOT: project["MEDIA_ROOT"] = OrderedDict([("path", settings.MEDIA_ROOT), ("disk", get_device_info(settings.MEDIA_ROOT))]) if config.STATIC_ROOT: project["STATIC_ROOT"] = OrderedDict([("path", settings.STATIC_ROOT), ("disk", get_device_info(settings.STATIC_ROOT))]) if config.CACHES: project["CACHES"] = get_caches_info() if config.installed_apps: project["installed_apps"] = get_installed_apps() if config.mail: project["mail"] = get_mail(**kwargs) return project
def full_wordcloud(): """ Generates wordcloud for the site. """ text = "" try: posts = Post.objects.filter().values("content") for post in posts: text += post["content"] + " " text = words_wo_stopwords(text=text) word_cloud = WordCloud(max_font_size=40, background_color="rgba(255, 255, 255, 0)", width=350, height=600, mode="RGBA").generate(text) fig = plt.figure(frameon=False) fig.patch.set_visible(False) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.imshow(word_cloud, interpolation='bilinear') plt.savefig(join(settings.STATIC_ROOT, 'images', 'wordcloud.png')) plt.close() except Exception as err: print(err)
def posts_wordcloud(): """ Generates wordcloud foeach post. """ posts = Post.objects.filter().exclude(content="") for post in posts: try: image_file = join(settings.STATIC_ROOT, "wordcloud", "{0}.png".format(post.slug)) if not isfile(image_file): text = words_wo_stopwords(text=post.content) if len(text) > 100: word_cloud = WordCloud(max_font_size=40, background_color="rgba(255, 255, 255, 0)", width=800, height=350, mode="RGBA").generate(text) fig = plt.figure(frameon=False) fig.patch.set_visible(False) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.imshow(word_cloud, interpolation='bilinear') plt.savefig(image_file) plt.close() post.wordcloud = "static/wordcloud/{0}.png".format(post.slug) post.save() except Exception as err: print(err)
def make_wordcloud(entry): """ Makes singular wordcloud for a post. """ text = words_wo_stopwords(text=entry.content) if len(text) > 100: word_cloud = WordCloud(max_font_size=60, background_color="rgba(255, 255, 255, 0)", mode="RGBA").generate(text) fig = plt.figure(frameon=False) fig.patch.set_visible(False) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.imshow(word_cloud, interpolation='bilinear') plt.savefig(join(settings.STATIC_ROOT, "wordcloud", "{0}.png".format(entry.slug))) plt.close() entry.wordcloud = "static/wordcloud/{0}.png".format(entry.slug) return entry
def download_cv_pdf(request, cv_id): cv = get_object_or_404(CV, pk=cv_id) response = HttpResponse(content_type="application/pdf") response["Content-Disposition"] = "attachment; filename=%s_%s.pdf" % (cv.first_name, cv.last_name) html = render_to_string("cv/cv_pdf.html", { "cv": cv, "MEDIA_ROOT": settings.MEDIA_ROOT, "STATIC_ROOT": settings.STATIC_ROOT, }) pdf = pisa.pisaDocument( StringIO(html.encode("UTF-8")), response, encoding="UTF-8", ) return response
def configure_from_settings(self, settings): # Default configuration self.charset = settings.FILE_CHARSET self.autorefresh = settings.DEBUG self.use_finders = settings.DEBUG self.static_prefix = urlparse(settings.STATIC_URL or '').path if settings.DEBUG: self.max_age = 0 # Allow settings to override default attributes for attr in self.config_attrs: settings_key = 'WHITENOISE_{0}'.format(attr.upper()) try: value = getattr(settings, settings_key) except AttributeError: pass else: value = decode_if_byte_string(value) setattr(self, attr, value) self.static_prefix = ensure_leading_trailing_slash(self.static_prefix) self.static_root = decode_if_byte_string(settings.STATIC_ROOT)
def __init__(self, apps=None, *args, **kwargs): # List of locations with static files self.locations = [] # Maps dir paths to an appropriate storage instance self.storages = SortedDict() if not isinstance(settings.STATICFILES_DIRS, (list, tuple)): raise ImproperlyConfigured( "Your STATICFILES_DIRS setting is not a tuple or list; " "perhaps you forgot a trailing comma?") for root in settings.STATICFILES_DIRS: if isinstance(root, (list, tuple)): prefix, root = root else: prefix = '' if os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root): raise ImproperlyConfigured( "The STATICFILES_DIRS setting should " "not contain the STATIC_ROOT setting") if (prefix, root) not in self.locations: self.locations.append((prefix, root)) for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super(FileSystemFinder, self).__init__(*args, **kwargs)