How can I change website logo for every other user login into django platform? For example: User-A when login, will see different logo on website, while User-B will see different and so on.
CodePudding user response:
Found this bit of code
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
CodePudding user response:
You could tie this into your database structure for the users themselves. In this case, I would recommend adding a picture field to a new model associated to the user (or better a group that the user is associated with):
class UserLogos(models.Model):
"""
Model for storing User Logos
"""
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
verbose_name=_("user"),
help_text=_("The associated user"),
)
photo = models.ImageField(
_("photo"),
upload_to="logo_photos",
help_text=_("Your logo photo"),
default="logo_photos/default.png",
)
Then in your views, simply call this item up:
def index(request):
"""
Index View
Render the server configured index page
"""
return render(
request,
"index.html",
{
"userLogo": models.UserLogos.filter(user=request.user).first()
},
)
And finally in your html template:
<img src="{{ userLogo.photo.url }}"/>
CodePudding user response:
Let's assume we have a route dynamic_logo_page/
which uses dynamic_logo_page.html
template and the corresponding view name is dynamic_logo_view
.
# Inside views.py
def get_logo_url(user_id):
# TODO: implement logic to get the logo_url based on user_id
return some_logo_url
def dynamic_logo_view(request):
if request.user.is_authenticated:
logo_url = get_logo_url(request.user.id)
content = {
"user_id": request.user.id,
"logo_url": logo_url
}
return render(request, 'dynamic_logo_page.html', content)
<!-- Inside dynamic_logo_page.html -->
...
{% if user.is_authenticated %}
<div>Hi {{ user_id }}, here is your personalized logo below:</div>
<img src="{{ logo_url }}" alt='logo'/>
{% endif %}
...
CodePudding user response:
The solution depends if you want a random logo shown to each user on every request, or if each user has their own specific logo.
Random logo for each user, different for each request
If you want the logo to be random for every request, you could provide a list of logos in the template context, and then use a random filter to pick a logo from the list at random.
<img id="logo" src="{% static logo_options|random %}">
This would result in a logo that statistically could be the same for two users at the same time, but that depends on how many logo variants are available.
Specific logo for each user, same for each request
To have the same logo for a user, then a logo must be specified and stored somehow. This could be achieved by adding a ImageField
to the User
model, and then retrieving this field in the template.
There are multiple ways to extend the User
model built into Django, see here for a tutorial on this: https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html
Depending on your solution, the template may end up looking something like this:
<img id="logo" src="{% static user.logo.url %}">