I have a javascript function in a django view that is supposed to redirect the user to a different URL that contains a query parameter. When I print out the url before assigning it to window.location, it matches the url pattern as expected. However once I assign the URL to window.location, it seems to append the url to the already existing one instead of replacing it, and thus cannot find the matching url pattern.
Javascript function:
function downloadEvidence() {
var product_review_ids = getProductReviewIDs();
if (product_review_ids === '') {
window.alert("Please select at least one deployment.")
return;
}
var url = window.location.href + "download_evidence?productReviewIDs=" + product_review_ids
console.log(url); //prints: http://127.0.0.1:8000/access-review-owner/configure/new%20test%20product/download_evidence?productReviewIDs=14
window.location = url;
}
The url pattern it is supposed to match:
urlpatterns = [
path('access-review-owner/configure/<str:product_name>/download_evidence/',views.download_evidence_view, name='download_evidence_view'),
]
The view it's supposed to navigate to:
@require_http_methods(['GET'])
def download_evidence_view(request, product_name):
product_id = request.GET.get('productReviewIDs', None)
print(product_id)
'''
NOTE: Package up evidence here
'''
return HttpResponseRedirect(os.environ['BASE_URL']+f'access-review-owner/configure/{product_name}/download_evidence/')
The url it's attempting to find when I assign the correct one to window.location:
GET http://127.0.0.1:8000/access-review-owner/configure/new%20test%20product/download_evidence/access-review-owner/configure/new%20test%20product/download_evidence/
Try to add this code
function downloadEvidence() {
var product_review_ids = getProductReviewIDs();
var product_name = //try to get you product name
if (product_review_ids === '') {
window.alert("Please select at least one deployment.")
return;
}
var url = `${window.location.origin}/${product_name}/download_evidence?productReviewIDs=${product_review_ids}`
console.log(url); //prints: http://127.0.0.1:8000/access-review-owner/configure/new%20test%20product/download_evidence?productReviewIDs=14
window.location.assign(url);
}