Skip to content

Commit

Permalink
Fix small bug where wrong username was displayed in some pages. See
Browse files Browse the repository at this point in the history
https://www.reddit.com/r/django/comments/f14wfi/whats_the_difference_between_user_and_requestuser/
for explanation. If "get_context_data" is used in a view, the "user" and
"request.user" can be different.
  • Loading branch information
elserj committed Nov 15, 2022
1 parent ee5fab2 commit bc65378
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion accounts/templates/accounts/password_reset_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
{% translate 'Your username, in case you’ve forgotten:' %} {{ request.user.get_username }}
{% translate "Thanks for using our site!" %}

{% blocktranslate %}The Planteome curate team{% endblocktranslate %}
Expand Down
2 changes: 1 addition & 1 deletion accounts/templates/accounts/user_edit_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block main %}
<div class="container" style="max-width: 40%">
<h2>Use this page to edit user info</h2>
<form action="{% url 'accounts:edit_user' pk=user.pk %}" method="post">
<form action="{% url 'accounts:edit_user' pk=request.user.pk %}" method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Submit" class="btn btn-primary">
Expand Down
8 changes: 4 additions & 4 deletions templates/footer.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{% load static %}
<div class="container" style="background: #e0e0e0; text-align: center">
<br>
{% if user.is_authenticated %}
{% if request.user.is_authenticated %}
<div class="row" style="align-items: center; justify-content: center">
<p>Logged in as {{ user.get_username }}
<p>Logged in as {{ request.user.username }}
<a href="{% url 'accounts:logout' %}" class="btn btn-primary" role="button"
style="margin-left: 5px">Logout?</a>
</p>
</div>
{% if user.role == "Superuser" %}
{% if request.user.role == "Superuser" %}
<div class="row" style="align-items: center; justify-content: center">
<p>Go to <a href="{% url 'superuser' %}" class="btn btn-secondary" role="button"
style="margin-left: 5px; margin-right: 5px">Superuser</a> page.
</p>
</div>
{% endif %}
{% if user.role == "Moderator" %}
{% if request.user.role == "Moderator" %}
<div class="row" style="align-items: center; justify-content: center">
<p>Go to <a href="{% url 'moderator' %}" class="btn btn-secondary" role="button"
style="margin-left: 5px; margin-right: 5px">Moderator</a> page.
Expand Down
2 changes: 1 addition & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<h1>
Welcome to the home page for planteome curate.
{% if not user.is_authenticated %}
<a href="{% url 'accounts:login' %}">Login</a>
<a href="{% url 'accounts:login' %}" class="btn btn-primary" role="button">Login</a>
{% endif %}
</h1>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/moderator.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block title %}Moderator page{% endblock %}

{% block main %}
{% if user.role == "Moderator" %}
{% if request.user.role == "Moderator" %}
<h2 align="center">All important pages for a moderator</h2>
<br>
<div class="card-deck">
Expand Down
4 changes: 2 additions & 2 deletions templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
</form>
</nav>
<div class="ml-auto" style="display: flex; justify-content: center; align-items: center">
{% if user.is_authenticated %}
{% if request.user.is_authenticated %}
<div class="col">
<div style="text-align: center">
<a class="fas fa-2x fa-user-alt" href="{% url 'accounts:edit_user' pk=user.pk %}"login button></a>
<a class="fas fa-2x fa-user-alt" href="{% url 'accounts:edit_user' pk=request.user.pk %}"login button></a>
</div>
<div style="text-align: center">Edit account</div>
</div>
Expand Down

0 comments on commit bc65378

Please sign in to comment.