Django Azure upload file to blob storage
Asked Answered
L

2

9

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]

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}/'

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

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)

I am running this in apache.(not runserver)

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

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

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


UPDATE

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'

Here's what it looks like in the browser

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'

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">
Lite answered 17/2, 2019 at 0:52 Comment(0)
L
11

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

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'

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)

you need to remove the MEDIA line:

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

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')

it all just worked.

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

python3 manage.py collectstatic

hope this helps other people!!!

Lite answered 19/2, 2019 at 13:54 Comment(1)
First of all, thanks for the answer because it helped. I think that just using AZURE_CONNECTION_STRING seems simpler.Sadfaced
F
3

I reviewed the source code azure_storage.py of jschneier/django-storages for Azure Storage and the document for Azure Storage, your issue was caused by the incorrect value of AZURE_LOCATION in settings.py which should be "" or a prefix string of blob as subfolder name in a container like <container name>/<prefix string as AZURE_LOCATION, such as A/B/..../ ><filename>.

The conclusion is based on the code analysis below.

The _get_file function atLINE 34 below used self._storage.service.get_blob_to_stream method of Storage SDK, and the value of parameter blob_name is self._path which appeared self._path = storage._get_valid_path(name) at LINE 32 inside __init__ function for 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

Then, the storage.__get_valid_path function with its dependent function _path at 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)

So you can see the code return blob_name from _path to _get_valid_path which joined AZURE_LOCATION with name. If use {AZURE_ACCOUNT_NAME}.blob.core.windows.net as location, the blob name for downloading will different from the blob name uploaded, not one to one correspondence.

Fahey answered 18/2, 2019 at 10:22 Comment(1)
Thank you! I have made the change of AZURE_LOCATION to "" and at least now I can get to the admin panel , but now when I just click on one of the MasterAccount records, the UI gives me an error: I'm getting the message that a positional argument name is missing (probably from the save() function. I've edited the question above with the details. thanks again. any idea on what could be going on?Lite

© 2022 - 2024 — McMap. All rights reserved.