更改Django中每个用户的网页徽标
我如何在其他用户登录到Django平台中更改 base.html 中的网站徽标?例如:user-a(request.user.customer.public_id)登录时,将其上传到文件夹中的徽标后,将在网站上看到不同的徽标,而用户-B将看到不同的徽标。 默认情况下,如果没有用户上传徽标,则他们将在网站上看到默认徽标。
<div class="container-fluid" title="Website">
<a data-testid="linkElement"
href="https://www.google.com" target="_self" class="xQ_iF"><img src="{% static 'logo.png' %}" alt="google"
style="width:90px;height:60px; margin-left: auto; margin-right: auto; padding-left: 32px; position: absolute;">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup" aria-expanded="true" aria-label="Toggle navigation">
</button>
</a>
<nav class="navbar navbar-expand-lg">
<ul class="navbar-nav ml-auto" style = "margin-left: auto;">
<li class="nav-item active">
<a class="nav-link" href="{% url 'home' %}"><i class="fa fa-home"></i> Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="https://www.google.com/support-page"><i class="fa fa-envelope"></i> Support<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink-4" data-bs-toggle="dropdown">
<i class="fa fa-user-circle-o"></i> {{request.user}}</a>
<div class="dropdown-menu" style="background-color: #
</div>
</li>
</ul>
</div>
</div>
</div>
</nav>
</div>
How can I change website logo in base.html for every other user login into Django platform? For example: User-A (request.user.customer.public_id) when login, will see different logo on website after uploading his logo in folder, while User-B will see different and so on.
By default, if no user upload logo, then they will see default logo on the website.
<div class="container-fluid" title="Website">
<a data-testid="linkElement"
href="https://www.google.com" target="_self" class="xQ_iF"><img src="{% static 'logo.png' %}" alt="google"
style="width:90px;height:60px; margin-left: auto; margin-right: auto; padding-left: 32px; position: absolute;">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup" aria-expanded="true" aria-label="Toggle navigation">
</button>
</a>
<nav class="navbar navbar-expand-lg">
<ul class="navbar-nav ml-auto" style = "margin-left: auto;">
<li class="nav-item active">
<a class="nav-link" href="{% url 'home' %}"><i class="fa fa-home"></i> Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="https://www.google.com/support-page"><i class="fa fa-envelope"></i> Support<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink-4" data-bs-toggle="dropdown">
<i class="fa fa-user-circle-o"></i> {{request.user}}</a>
<div class="dropdown-menu" style="background-color: #
</div>
</li>
</ul>
</div>
</div>
</div>
</nav>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将其与用户本身的数据库结构联系在一起。在这种情况下,我建议将图片字段添加到与用户关联的新模型(或与用户关联的组相关联):
然后在您的视图中,只需调用此项目:
最后在您的HTML模板中:
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):
Then in your views, simply call this item up:
And finally in your html template:
假设我们有一个路由
dynamic_logo_page/
使用dynamic_logo_page.html
模板,相应的视图名称为dynamic_logo_view
。Let's assume we have a route
dynamic_logo_page/
which usesdynamic_logo_page.html
template and the corresponding view name isdynamic_logo_view
.解决方案取决于您是否要在每个请求上向每个用户显示一个随机徽标,或者每个用户都有自己的特定徽标。
每个用户的随机徽标,每个请求都不同,
如果您希望徽标是每个请求的随机徽标,则可以在模板上下文中提供徽标列表,然后使用a 随机过滤器从列表中随机选择徽标。
这将导致两个用户同时在统计上相同的徽标,但这取决于有多少徽标变体可用。
每个用户的特定徽标,每个请求都具有
为用户具有相同徽标的特定徽标,然后必须以某种方式指定并存储徽标。这可以通过将
imageField
添加到用户
模型,然后在模板中检索此字段来实现。有多种方法可以扩展
用户
内置在Django中的模型,请参见此处的教程: https://simpleisbetterterthancomplex.com/tutorial/2016/07/07/22/how-to-to-to-to-to-to--extend-django-django-user-user-model-model.html < /a>根据您的解决方案,模板最终可能会看起来像这样:
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.
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 theUser
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.htmlDepending on your solution, the template may end up looking something like this:
这就是我解决这个问题的方式。
这是我的型号。
这是保存徽标的功能。
我的设置.py
这是HTML模板
This is how I solved this issue.
Here is my models.py
Here is the function where logos will be saved.
My settings.py
Here is HTML template
我找到了解决这个问题的另一种替代方法。以下是我的型号。
HTML模板
I found one more alternative way to solve this problem. Below is my models.py file.
HTML Template