Reputation: 385
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:
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'
Upvotes: 1
Views: 633
Reputation: 385
I've figured out what was the problem after seing the detailed logs that wholevinski helped me finding with his comments plus his assistance. I don't like to answer my own questions, but I'll do it here to keep a record of how I solved the problem.
Well, what is happening is that I am using WhiteNoise to serve my static files and it's compressing my static files to be able to have cache support.
In my code I have:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
The following line in my code makes whitenoise create file versions with the md5 hash of the file content appended to the original file name, so for instance, "filename.jpeg" generates a file "filename.674506bb8a45.jpeg" that will be served to the user. If the server file is different from what is being cached in the user browser, so the md5 will be different and the new file will be served to the user.
The problem was that I was getting the image filename via javascript and it was saving in my database:
account_settings/avatar-images/man2.674506bb8a45.jpeg
instead of
account_settings/avatar-images/man2.jpeg
What I did to solve the problem was change my javascript to ignore the md5 version. Now I am saving account_settings/avatar-images/man2.jpeg and the problem is gone.
Upvotes: 2
Reputation: 20339
Try this
{% if public_user.profile.avatar %}
{% with public_user.profile.avatar as img_url %}
<img class="img-fluid w-100 u-block-hover__main--zoom-v1" src="{% static img_url %}" alt="User Avatar Image">
{% endwith %}
{% 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 %}
Upvotes: 0
Reputation: 5300
Try to add the admin email to your setting, this will provide you a way to determine what is the error message when error 500 occurs
SERVER_EMAIL = '[email protected]' ADMINS = ( ('Exceptions Email', '[email protected]'), )
you might need to setup smtp setting as well
Upvotes: 0