Django Azure загрузить файл в хранилище BLOB-объектов - PullRequest
0 голосов
/ 17 февраля 2019

У меня есть приложение, которое должно загрузить файл в контейнер хранилища BLOB-объектов Azure.Я очень долго пытался и следовал каждому учебнику безрезультатно.

Я установил следующее:

pip3 install django-storages[azure]

Вот мой файл settings.py:

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 <- мой класс хранения для клиентов </p>

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:

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)

IЯ запускаю это в Apache. (не runserver)

ПРОБЛЕМА: я выяснил, что с этой конфигурацией я могу получить все файлы из хранилища BLOB-объектов, включая медиа-файлы и статические файлы.Все они появляются идеально.Однако, когда я пытаюсь добавить и сохранить экземпляр MasterAccount, он barfs с ошибкой абсолютного пути:

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», но не делает »не знаете куда его отправить?

Нужно ли менять настройки в лазури?Мой конфиг все не так?Пожалуйста помогите, я ломал голову над этим и утомлял Google в течение приблизительно 3 недель!Спасибо !!!


ОБНОВЛЕНИЕ

После полезного предложения, приведенного ниже, я могу, по крайней мере, теперь войти в панель администратора.но всякий раз, когда я пытаюсь отредактировать модель MasterAccount, нажимая на нее, я получаю ошибку typeError:

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

Вот среда обратного копирования при копировании и вставке:

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:

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 () не работает должным образом:

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">

Ответы [ 2 ]

0 голосов
/ 19 февраля 2019

Разобрался с проблемой.Я следовал руководству по Amazon S3, а затем применил те же принципы к сценарию Azure.В конце концов, я уверен, что разработчик этого класса хотел, чтобы все было одинаково.

mysite / custom_azure.py <- просто поместите его в ту же папку, что и ваш файл settings.py </p>

from storages.backends.azure_storage import AzureStorage

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

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 из какого-то другого учебника или чего-то еще:

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

вам нужно удалить строку MEDIA:

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

из модели, удалить любую ссылку на «хранилище» и просто оставить опцию «upload_to» следующим образом:

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

все этопросто работал.

не заставляйте себя делать следующее, чтобы проверить себя:

python3 manage.py collectstatic

надеюсь, это поможет другим людям !!!

0 голосов
/ 18 февраля 2019

Я просмотрел исходный код azure_storage.py из jschneier/django-storages для хранилища Azure и документ для Хранение Azure , ваша проблема вызвана невернойзначение AZURE_LOCATION в settings.py, которое должно быть "" или префиксная строка большого двоичного объекта в качестве имени подпапки в контейнере, например <container name>/<prefix string as AZURE_LOCATION, such as A/B/..../ ><filename>.

Вывод основан на анализе кода ниже.

Функция _get_file при LINE 34 ниже использовала self._storage.service.get_blob_to_stream метод Storage SDK, а значение параметра blob_name равно self._path, которое появилось self._path = storage._get_valid_path(name) при LINE 32 внутри __init__ функции дляAzureStorageFile:

#LINE 32 inside def __init__(self, name, mode, storage):
    self._path = storage._get_valid_path(name)

# LINE 34
def _get_file(self):
    if self._file is not None:
        return self._file

    file = SpooledTemporaryFile(
        max_size=self._storage.max_memory_size,
        suffix=".AzureStorageFile",
        dir=setting("FILE_UPLOAD_TEMP_DIR", None))

    if 'r' in self._mode or 'a' in self._mode:
        # I set max connection to 1 since spooledtempfile is
        # not seekable which is required if we use max_connections > 1
        self._storage.service.get_blob_to_stream(
            container_name=self._storage.azure_container,
            blob_name=self._path,
            stream=file,
            max_connections=1,
            timeout=10)
    if 'r' in self._mode:
        file.seek(0)

    self._file = file
    return self._file

Затем функция storage.__get_valid_path с зависимой функцией _path в LINE 173:

#LINE 147
location = setting('AZURE_LOCATION', '')

#LINE 173
def _path(self, name):
    name = _clean_name_dance(name)
    try:
        return safe_join(self.location, name)
    except ValueError:
        raise SuspiciousOperation("Attempted access to '%s' denied." % name)

def _get_valid_path(self, name):
    # Must be idempotent
    return _get_valid_path(self._path(name))

def _open(self, name, mode="rb"):
    return AzureStorageFile(name, mode, self)

def get_valid_name(self, name):
    return _clean_name_dance(name)

def get_available_name(self, name, max_length=_AZURE_NAME_MAX_LEN):
    """
    Returns a filename that's free on the target storage system, and
    available for new content to be written to.
    """
    name = self.get_valid_name(name)
    if self.overwrite_files:
        return get_available_overwrite_name(name, max_length)
    return super(AzureStorage, self).get_available_name(name, max_length)

Таким образом, вы можете увидеть код возврата blob_name от _path до _get_valid_path, который присоединился к AZURE_LOCATION с name.Если использовать {AZURE_ACCOUNT_NAME}.blob.core.windows.net в качестве location, имя загружаемого большого двоичного объекта будет отличаться от загруженного имени большого двоичного объекта, а не однозначного соответствия.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...