I am using Django 1.11 in my application.
I've implemented the authentication using django-registration and created a profile model to store some user infos:
class Profile(models.Model):
...
user = models.OneToOneField(User)
nick = models.CharField(max_length=50)
level = models.PositiveIntegerField(null=True)
avatar = models.CharField(max_length=500, null=True, blank=True)
...
This model is being created/saved with signals:
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
post_save.connect(create_user_profile, sender=User)
post_save.connect(save_user_profile, sender=User)
Well, I am not allowing users to upload images, as you see. The workflow of choosing an avatar image is:
- User access profile configuration page
- This page has a button that opens a modal with image options, the user has to choose one.
- User select an image.
- User click to save changes.
What I am saving at profile's avatar field is a string that have to be concatenate to the static url, for instance, if the image path is:
127.0.0.1:8000/static/account_settings/avatar-images/man2.jpeg
I am saving:
account_settings/avatar-images/man2.jpeg
I've being through this workflow in production with debug set to True (impossible to make with debug = False because of the error 500). So, I've opened the user public profile page and it gives me the same error.
But I've found the root of the problem in this template:
{% if public_user.profile.avatar %}
<img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static public_user.profile.avatar %}" alt="User Avatar Image">
{% else %}
<img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static 'assets/img/tmp/avatar.jpg' %}" alt="User Avatar Image">
{% endif %}
If exist something in public_user.profile.avatar, I have the error 500. But if the profile has no image, it works fine. I don't know why this code doesn't work:
{% static public_user.profile.avatar %}
Any ideia why the result of this code is getting me error?
Error log:
2018-01-10T13:53:05.153051+00:00 app[web.1]: url = self.url(context)
2018-01-10T13:53:05.153052+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py", line 102, in url
2018-01-10T13:53:05.153053+00:00 app[web.1]: return self.handle_simple(path)
2018-01-10T13:53:05.153053+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py", line 117, in handle_simple
2018-01-10T13:53:05.153054+00:00 app[web.1]: return staticfiles_storage.url(path)
2018-01-10T13:53:05.153055+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 162, in url
2018-01-10T13:53:05.153055+00:00 app[web.1]: return self._url(self.stored_name, name, force)
2018-01-10T13:53:05.153056+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 141, in _url
2018-01-10T13:53:05.153057+00:00 app[web.1]: hashed_name = hashed_name_func(*args)
2018-01-10T13:53:05.153057+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 432, in stored_name
2018-01-10T13:53:05.153063+00:00 app[web.1]: raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
2018-01-10T13:53:05.153064+00:00 app[web.1]: ValueError: Missing staticfiles manifest entry for 'account_settings/avatar-images/man2.674506bb8a45.jpeg'