Django Azure将文件上传到Blob存储 [英] Django Azure upload file to blob storage

查看:94
本文介绍了Django Azure将文件上传到Blob存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要将文件上传到Azure Blob存储容器的应用程序.我已经尝试了很长时间,并没有按照每个教程进行操作.

I've got an application that needs to upload a file to an Azure Blob Storage container. I've been trying for a very long time and followed every tutorial to no avail.

我已经安装了以下内容:

I have installed the following:

pip3 install django-storages[azure]

这是我的settings.py文件:

Here is my settings.py file:

DEFAULT_FILE_STORAGE = 'backend.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage'

STATIC_LOCATION = "static"
MEDIA_LOCATION = "http://{AZURE_ACCOUNT_NAME}.blob.core.windows.net/media"
MEDIA_ROOT='http://{AZURE_ACCOUNT_NAME}.blob.core.windows.net'

AZURE_ACCOUNT_NAME = '<mystorageaccount>'
AZURE_ACCOUNT_KEY = '<my key here>'
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
AZURE_LOCATION=f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'

STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'

这是.backend/custom_azure.py<-我的客户存储类

Here is the .backend/custom_azure.py <- my customer storage class

from storages.backends.azure_storage import AzureStorage

class AzureMediaStorage(AzureStorage):
    account_name = '<mystorageaccount>' 
    account_key = '<mykey>'
    azure_container = 'media'
    expiration_secs = None

class AzureStaticStorage(AzureStorage):
    account_name = 'mystorageaccount'
    account_key = '<my key>'
    azure_container = 'static'
    expiration_secs = None

这是我的.account/models.py:

Here is my .account/models.py:

from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User
from billing.models import Subs
from PIL import Image
import phonenumbers
from backend.custom_azure import AzureMediaStorage as AMS

class MasterAccount(models.Model):
    image = models.ImageField(default='default.jpg', storage=AMS, upload_to='profile_pics')
    profile_pic = models.FileField(default='default.jpg', storage=AMS, upload_to='profile_pics')

    def __str__(self):
        return "MasterAccount Number: " + str(self.number)

    def save(self, *args ,**kwargs):
        super().save(*args ,**kwargs)
        img =  Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

我正在apache中运行它(不是runserver)

I am running this in apache.(not runserver)

问题:我已经发现,使用此配置,我能够从Blob存储中检索所有文件,包括媒体文件和静态文件.它们都完美地显示出来.但是,当我尝试添加和保存MasterAccount实例时,它会出现绝对路径错误而倒钩:

PROBLEM: I have figured out that with this configuration, I am able to retrieve all of the files from blob storage including the media files and the static files. They all show up perfectly. However, when I try to add and save a MasterAccount instance it barfs with the absolute paths error:

NotImplementedError at /admin/account/masteraccount/2/change/
This backend doesn't support absolute paths.
Request Method: POST
Request URL:    http://13.67.214.185/nbn/admin/account/masteraccount/2/change/
Django Version: 2.1.7
Exception Type: NotImplementedError
Exception Value:    
**This backend doesn't support absolute paths**.
Exception Location: /opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.1.7-py3.7.egg/django/core/files/storage.py in path, line 106
Python Executable:  /opt/bitnami/python/bin/python3
Python Version: 3.7.2
Python Path:    
['/opt/bitnami/python/lib/python37.zip',
 '/opt/bitnami/python/lib/python3.7',
 '/opt/bitnami/python/lib/python3.7/lib-dynload',
 '/opt/bitnami/python/lib/python3.7/site-packages',
 '/opt/bitnami/apps/django/lib/python3.7/site-packages',
 '/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.1.7-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/pytz-2016.7-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/gevent-1.3.7-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/olefile-0.46-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/Pillow-4.0.0-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/lxml-4.2.5-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/mysqlclient-1.3.12-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/psycopg2-2.7.3.1-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/html5lib-0.90-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/python_Levenshtein-0.12.0-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/scandir-1.5-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/argparse-1.2.1-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/pyparsing-2.1.10-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/simplejson-3.11.1-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/pyldap-2.4.28-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/reportlab-3.3.0-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/pydot-1.2.3-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/MarkupSafe-0.23-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/psutil-4.3.1-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/pytz-2016.7-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/six-1.10.0-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/libsass-0.15.1-py3.7-linux-x86_64.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/virtualenv_clone-0.2.6-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/virtualenv-15.1.0-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/jmespath-0.9.3-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/python_dateutil-2.6.1-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/botocore-1.7.43-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/s3transfer-0.1.11-py3.7.egg',
 '/opt/bitnami/python/lib/python3.7/site-packages/boto3-1.4.7-py3.7.egg',
 '/opt/bitnami/apps/django/django_projects/nbn']
Server time:    Sat, 16 Feb 2019 21:38:11 +0000

所以看起来无论如何,它只是传递"default.jpg"文件名,但不知道将文件发送到何处?

So it looks like no matter what, it's just passing the 'default.jpg' filename but doesn't know where to send it?

我是否需要更改天蓝色的设置?我的配置是否全部错误?请帮助,我已经为此动了脑筋,用了3个星期使Google筋疲力尽!谢谢!!!

Do I need to change settings in azure? Is my config all wrong? Please help, I've been wracking my brain on this one and exhausting google for about 3 weeks! Thanks!!!

在下面有帮助的建议之后,我至少现在可以登录到管理面板.但是每当我尝试通过单击它来编辑MasterAccount模型时,都会出现typeError错误:

After the helpful suggestion below I can at least now log into the admin panel. but whenever I try to edit a MasterAccount model by clicking on it, I get a typeError error:

TypeError at /admin/account/masteraccount/5/change/
url() missing 1 required positional argument: 'name'
Request Method: GET
Request URL:    http://localhost:8000/admin/account/masteraccount/5/change/
Django Version: 2.1.4
Exception Type: TypeError
Exception Value:    
url() missing 1 required positional argument: 'name'
Exception Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/files.py in url, line 62
Python Executable:  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Python Version: 3.7.2
Python Path:    
['/Users/elliottcastillo/Documents/azure-git/trt/src',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
Server time:    Mon, 18 Feb 2019 13:46:22 +0000

这是复制粘贴回溯 环境:

Here is the copy-paste traceback Environment:

Request Method: GET
Request URL: http://localhost:8000/admin/account/masteraccount/5/change/

Django Version: 2.1.4
Python Version: 3.7.2
Installed Applications:
['account.apps.AccountConfig',
 'administrative.apps.AdministrativeConfig',
 'users.apps.UsersConfig',
 'blog.apps.BlogConfig',
 'billing.apps.BillingConfig',
 'videos.apps.VideosConfig',
 'stats.apps.StatsConfig',
 'train.apps.TrainConfig',
 'external_content.apps.ExternalContentConfig',
 'tinymce',
 'phone_field',
 'phonenumbers',
 'colorfield',
 'djmoney',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'storages',
 'azure']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
   url() missing 1 required positional argument: 'name'
   9 :             {% for field in line %}
   10 :                 <div{% if not line.fields|length_is:'1' %} class="fieldBox{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
   11 :                     {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
   12 :                     {% if field.is_checkbox %}
   13 :                         {{ field.field }}{{ field.label_tag }}
   14 :                     {% else %}
   15 :                         {{ field.label_tag }}
   16 :                         {% if field.is_readonly %}
   17 :                             <div class="readonly">{{ field.contents }}</div>
   18 :                         {% else %}
   19 :                              {{ field.field }} 
   20 :                         {% endif %}
   21 :                     {% endif %}
   22 :                     {% if field.field.help_text %}
   23 :                         <div class="help">{{ field.field.help_text|safe }}</div>
   24 :                     {% endif %}
   25 :                 </div>
   26 :             {% endfor %}
   27 :         </div>
   28 :     {% endfor %}
   29 : </fieldset>


Traceback:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  156.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  154.                 response = response.render()

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/response.py" in render
  106.             self.content = self.rendered_content

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  171.                     return self._render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  188.             return template.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  173.                 return self._render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  309.                 return nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  309.                 return nodelist.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render
  993.         return render_value_in_context(output, context)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py" in render_value_in_context
  972.             value = str(value)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/html.py" in <lambda>
  397.     klass.__str__ = lambda self: mark_safe(klass_str(self))

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/boundfield.py" in __str__
  33.         return self.as_widget()

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/boundfield.py" in as_widget
  93.             renderer=self.form.renderer,

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/widgets.py" in render
  229.         context = self.get_context(name, value, attrs)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/widgets.py" in get_context
  415.         context = super().get_context(name, value, attrs)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/widgets.py" in get_context
  281.         context = super().get_context(name, value, attrs)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/widgets.py" in get_context
  221.             'value': self.format_value(value),

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/widgets.py" in format_value
  411.         if self.is_initial(value):

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/forms/widgets.py" in is_initial
  405.         return bool(value and getattr(value, 'url', False))

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/files.py" in url
  62.         return self.storage.url(self.name)

Exception Type: TypeError at /admin/account/masteraccount/5/change/
Exception Value: url() missing 1 required positional argument: 'name'

这是浏览器中的外观

当我尝试加载前端时,在django中收到以下错误:

When I try to load the front-end I get the following error in django:

Internal Server Error: /
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
TypeError: 'ImageFieldFile' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/elliottcastillo/Documents/azure-git/trt/src/account/views.py", line 49, in index
    return render(request, 'account/index.html', context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/template/base.py", line 837, in _resolve_lookup
    current = getattr(current, bit)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/files.py", line 62, in url
    return self.storage.url(self.name)
TypeError: url() missing 1 required positional argument: 'name'

以下是来自浏览器的消息...似乎url()方法无法正常工作:

Here is the message from the browser... It seems that the url() method is not acting properly:

Error during template rendering
In template /Users/elliottcastillo/Documents/azure-git/trt/src/account/templates/account/base2.html, error at line 178

url() missing 1 required positional argument: 'name'
168                                                 <input class="rd-navbar-search-form-input form-input" id="rd-navbar-search-form-input" type="text" name="s" autocomplete="off">
169                                                 <div class="rd-search-results-live" id="rd-search-results-live"></div>
170                                             </div>
171                                             <button class="rd-search-form-submit fl-budicons-launch-search81" type="submit"></button>
172                                         </form>
173                                     </div>
174                                 </li>
175                                 {% if user.is_authenticated %}
176                                     <li class="rd-nav-item">
177                                         <a class="rd-nav-link" href="{% url 'users:profile' %}">
178                                             <img class="rounded-circle border border-info mr-2" src="{{ user.masteraccount.image.url }}" width="50" height="50">
179                                             Account
180                                         </a>
181                                         <!-- RD Navbar Dropdown-->
182                                         <ul class="rd-menu rd-navbar-dropdown">
183                                             <li class="rd-dropdown-item">
184                                                 <a class="rd-dropdown-link" href="{% url 'users:profile' %}">
185                                                     My Profile
186                                                 </a>
187                                             </li>
188                                             <li class="rd-dropdown-item">

推荐答案

找出了问题所在.我遵循了有关Amazon S3的教程,然后将完全相同的主体应用于Azure场景.毕竟,我确定此类的开发人员希望使所有内容保持一致.

Figured out the problem. I followed a tutorial for Amazon S3 and then applied the exact same principals to the Azure scenario. After all, I'm sure the developer of this class wanted to keep everything uniform.

mysite/custom_azure.py<-只需将其与settings.py文件放在同一文件夹中

mysite/custom_azure.py <-- just put it in the same folder as your settings.py file

from storages.backends.azure_storage import AzureStorage

class AzureMediaStorage(AzureStorage):
    location = 'media'
    file_overwrite = False

mysite/settings.py

mysite/settings.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, '<directory that houses the static files>/static'),
]

AZURE_ACCOUNT_NAME = '<azure container name>'
AZURE_ACCOUNT_KEY = '<azure account key for this container>'
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
AZURE_LOCATION = '<blob container name>'
AZURE_CONTAINER = '<blob container name>'

STATIC_LOCATION = 'static'
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'

STATICFILES_STORAGE = 'storages.backends.azure_storage.AzureStorage'
DEFAULT_FILE_STORAGE = 'mysite.custom_azure.AzureMediaStorage'

另外,请注意,如果您的mysite/urls.py中包含其他一些教程中的内容或其他内容,则为

Also, note that if you have the following in your mysite/urls.py from some other tutorial or something:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您需要删除MEDIA行:

you need to remove the MEDIA line:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

从模型中删除对存储"的任何引用,然后像这样保留"upload_to"选项:

from the model, remove any reference to 'storage' and just leave the 'upload_to' option like this:

thumbnail = models.ImageField(default='default.jpg', upload_to='video_thumbs')

这一切都奏效了.

请不要执行以下操作来检查自己:

don't for get to do the following to check yourself:

python3 manage.py collectstatic

希望这对其他人有帮助!

hope this helps other people!!!

这篇关于Django Azure将文件上传到Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆