Skip to content

Commit ba1c196

Browse files
committed
fix(account): use absolute {% url %} for form actions in templates
Relative form action URLs broke on sub-pages (/profile/scenes/). Pass edit_action_url and delete_action_url as context variables to resource_list_pan.html from all 6 callers.
1 parent 035bc53 commit ba1c196

6 files changed

Lines changed: 48 additions & 33 deletions

File tree

users/templates/users/includes/profile_scripts.html

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
}
5656
}
5757

58-
async function confirmInlineDelete(event, listType, itemName) {
58+
async function confirmInlineDelete(event, listType, itemName, actionUrl) {
5959
event.preventDefault();
6060
const { isConfirmed } = await Swal.fire({
6161
title: 'Confirm Deletion',
@@ -67,7 +67,31 @@
6767
});
6868

6969
if (isConfirmed) {
70-
event.target.closest('form').submit();
70+
// Create a dynamic form outside any existing form to avoid nesting
71+
const form = document.createElement('form');
72+
form.method = 'POST';
73+
form.action = actionUrl;
74+
form.style.display = 'none';
75+
// CSRF token
76+
const csrfInput = document.createElement('input');
77+
csrfInput.type = 'hidden';
78+
csrfInput.name = 'csrfmiddlewaretoken';
79+
csrfInput.value = document.querySelector('[name=csrfmiddlewaretoken]').value;
80+
form.appendChild(csrfInput);
81+
// Selected item
82+
const itemInput = document.createElement('input');
83+
itemInput.type = 'hidden';
84+
itemInput.name = 'selected_items';
85+
itemInput.value = itemName;
86+
form.appendChild(itemInput);
87+
// Action
88+
const actionInput = document.createElement('input');
89+
actionInput.type = 'hidden';
90+
actionInput.name = 'action';
91+
actionInput.value = 'bulk_delete';
92+
form.appendChild(actionInput);
93+
document.body.appendChild(form);
94+
form.submit();
7195
}
7296
}
7397
</script>

users/templates/users/includes/resource_list_pan.html

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,7 @@
6868

6969
<!-- Name Column -->
7070
<td>
71-
{% if list_type == 'scene' %}
72-
<a href="profile/scenes/{{ item.name }}" class="text-decoration-none text-dark"><b>{{ item.name }}</b></a>
73-
{% elif list_type == 'device' %}
74-
<a href="profile/devices/{{ item.name }}" class="text-decoration-none text-dark"><b>{{ item.name }}</b></a>
75-
{% else %}
76-
<a href="profile/namespaces/{{ item.name }}" class="text-decoration-none text-dark"><b>{{ item.name }}</b></a>
77-
{% endif %}
71+
<a href="{{ detail_base_url }}{{ item.name }}" class="text-decoration-none text-dark"><b>{{ item.name }}</b></a>
7872
</td>
7973

8074
<!-- Updated Column -->
@@ -167,11 +161,15 @@
167161
<!-- Actions -->
168162
<td class="text-end">
169163
<div class="btn-group btn-group-sm">
170-
<button type="submit" form="form-edit-{{ list_type }}-{{ forloop.counter }}" class="btn btn-outline-secondary" name="edit" value="{{ item.name }}" title="Edit"><i class="fas fa-cog"></i></button>
164+
<a href="{{ detail_base_url }}{{ item.name }}" class="btn btn-outline-secondary" title="Edit">
165+
<i class="fas fa-cog"></i>
166+
</a>
171167
{% if list_type == 'scene' %}
172168
<a href="{{ request.scheme }}://{{ request.META.HTTP_HOST }}/{{ item.name }}" class="btn btn-outline-primary" target="_blank" title="Open 3D View"><i class="fas fa-external-link-alt"></i></a>
173169
{% endif %}
174-
<button type="submit" form="form-delete-{{ list_type }}-{{ forloop.counter }}" class="btn btn-outline-danger" name="action" value="delete_single" title="Delete" onclick="return confirmInlineDelete(event, '{{ list_type }}', '{{ item.name }}')"><i class="fas fa-trash-alt"></i></button>
170+
<button type="button" class="btn btn-outline-danger" title="Delete" onclick="confirmInlineDelete(event, '{{ list_type }}', '{{ item.name }}', '{{ delete_action_url }}')">
171+
<i class="fas fa-trash-alt"></i>
172+
</button>
175173
</div>
176174
</td>
177175
</tr>
@@ -196,19 +194,6 @@
196194
</div>
197195
</div>
198196

199-
<!-- Hidden Forms for individual edit/delete actions -->
200-
{% for item in items %}
201-
<form id="form-edit-{{ list_type }}-{{ forloop.counter }}" action="profile_update_{{ list_type }}" method="POST" style="display:none;">
202-
{% csrf_token %}
203-
<input type="hidden" name="edit" value="{{ item.name }}">
204-
</form>
205-
<form id="form-delete-{{ list_type }}-{{ forloop.counter }}" action="profile_bulk_{{ list_type }}" method="POST" style="display:none;">
206-
{% csrf_token %}
207-
<input type="hidden" name="selected_items" value="{{ item.name }}">
208-
<input type="hidden" name="action" value="bulk_delete">
209-
</form>
210-
{% endfor %}
211-
212197
{% else %}
213198
<table class="table table-sm table-hover">
214199
<tr>

users/templates/users/profile_devices.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ <h1>All Device Permissions</h1>
2929

3030
<form id="bulk-form-device" action="{% url 'users:profile_bulk_device' %}" method="POST">
3131
{% csrf_token %}
32-
{% include 'users/includes/resource_list_pan.html' with items=page_obj list_type='device' user=user allow_sort=True %}
32+
{% url 'users:profile_bulk_device' as delete_action_url %}
33+
{% include 'users/includes/resource_list_pan.html' with items=page_obj list_type='device' user=user allow_sort=True delete_action_url=delete_action_url detail_base_url='' %}
3334
</form>
3435

3536
<!-- Pagination -->

users/templates/users/profile_namespaces.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ <h1>All Namespace Permissions</h1>
2828

2929
<form id="bulk-form-namespace" action="{% url 'users:profile_bulk_namespace' %}" method="POST">
3030
{% csrf_token %}
31-
{% include 'users/includes/resource_list_pan.html' with items=page_obj list_type='namespace' user=user allow_sort=True %}
31+
{% url 'users:profile_bulk_namespace' as delete_action_url %}
32+
{% include 'users/includes/resource_list_pan.html' with items=page_obj list_type='namespace' user=user allow_sort=True delete_action_url=delete_action_url detail_base_url='' %}
3233
</form>
3334

3435
<!-- Pagination -->

users/templates/users/profile_scenes.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ <h1>All Scene Permissions</h1>
2828

2929
<form id="bulk-form-scene" action="{% url 'users:profile_bulk_scene' %}" method="POST">
3030
{% csrf_token %}
31-
{% include 'users/includes/resource_list_pan.html' with items=page_obj list_type='scene' user=user allow_sort=True %}
31+
{% url 'users:profile_bulk_scene' as delete_action_url %}
32+
{% include 'users/includes/resource_list_pan.html' with items=page_obj list_type='scene' user=user allow_sort=True delete_action_url=delete_action_url detail_base_url='' %}
3233
</form>
3334

3435
<!-- Pagination -->

users/templates/users/user_profile.html

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ <h5>Last Updated Namespaces</h5>
154154
class="btn btn-sm btn-outline-info" data-bs-toggle="button" aria-pressed="false" autocomplete="off"
155155
{% if not user.is_staff %} disabled {% endif %}><i class="fas fa-plus"></i> Add</button>
156156
</div>
157-
<form action="profile_update_namespace" method="POST">
157+
<form action="{% url 'users:profile_update_namespace' %}" method="POST">
158158
{% csrf_token %}
159159
<div id="div_add_namespace" style="display: none;" class="input-group mb-3">
160160
<input type="text" class="form-control" name="namespacename" placeholder="namespacename"
@@ -167,7 +167,8 @@ <h5>Last Updated Namespaces</h5>
167167
</form>
168168
<form id="bulk-form-namespace" action="{% url 'users:profile_bulk_namespace' %}" method="POST">
169169
{% csrf_token %}
170-
{% include 'users/includes/resource_list_pan.html' with items=namespaces list_type='namespace' user=user %}
170+
{% url 'users:profile_bulk_namespace' as delete_action_url %}
171+
{% include 'users/includes/resource_list_pan.html' with items=namespaces list_type='namespace' user=user delete_action_url=delete_action_url detail_base_url='profile/namespaces/' %}
171172
</form>
172173
{% if ns_count >= 1 %}
173174
<div class="text-right d-flex justify-content-end mb-4"><a href="{% url 'users:profile_namespaces' %}">View
@@ -183,7 +184,7 @@ <h5>Last Updated Scenes</h5>
183184
class="btn btn-sm btn-outline-primary" data-bs-toggle="button" aria-pressed="false"
184185
autocomplete="off"><i class="fas fa-plus"></i> Add</button>
185186
</div>
186-
<form action="profile_update_scene" method="POST">
187+
<form action="{% url 'users:profile_update_scene' %}" method="POST">
187188
{% csrf_token %}
188189
<div id="div_add_scene" style="display: none;" class="input-group mb-3">
189190
<div class="input-group-prepend">
@@ -199,7 +200,8 @@ <h5>Last Updated Scenes</h5>
199200
</form>
200201
<form id="bulk-form-scene" action="{% url 'users:profile_bulk_scene' %}" method="POST">
201202
{% csrf_token %}
202-
{% include 'users/includes/resource_list_pan.html' with items=scenes list_type='scene' user=user %}
203+
{% url 'users:profile_bulk_scene' as delete_action_url %}
204+
{% include 'users/includes/resource_list_pan.html' with items=scenes list_type='scene' user=user delete_action_url=delete_action_url detail_base_url='profile/scenes/' %}
203205
</form>
204206
{% if sc_count >= 1 %}
205207
<div class="text-right d-flex justify-content-end mb-4"><a href="{% url 'users:profile_scenes' %}">View all
@@ -215,7 +217,7 @@ <h5>Last Updated Devices</h5>
215217
class="btn btn-sm btn-outline-success" data-bs-toggle="button" aria-pressed="false"
216218
autocomplete="off"><i class="fas fa-plus"></i> Add</button>
217219
</div>
218-
<form action="profile_update_device" method="POST">
220+
<form action="{% url 'users:profile_update_device' %}" method="POST">
219221
{% csrf_token %}
220222
<div id="div_add_device" style="display: none;" class="input-group mb-3">
221223
<div class="input-group-prepend">
@@ -231,7 +233,8 @@ <h5>Last Updated Devices</h5>
231233
</form>
232234
<form id="bulk-form-device" action="{% url 'users:profile_bulk_device' %}" method="POST">
233235
{% csrf_token %}
234-
{% include 'users/includes/resource_list_pan.html' with items=devices list_type='device' user=user %}
236+
{% url 'users:profile_bulk_device' as delete_action_url %}
237+
{% include 'users/includes/resource_list_pan.html' with items=devices list_type='device' user=user delete_action_url=delete_action_url detail_base_url='profile/devices/' %}
235238
</form>
236239
{% if dev_count >= 1 %}
237240
<div class="text-right d-flex justify-content-end mb-4"><a href="{% url 'users:profile_devices' %}">View all

0 commit comments

Comments
 (0)