Reputation: 5540
I have a table with multiple rows, each of which has an 'edit' button. On clicking this button, the user should redirect to a form with his details already prefilled. Now, I have defined data attributes inside the 'edit' button's definition:
<a href='#' class="edit-user" data-email="[email protected]">edit</a>
Using JQuery, I'm deriving the value of the 'email' data attribute and passing it in a POST request to /add-edit-user
, which is also mapped to the page which has my form:
$( ".edit-user" ).click(function()
{
var url = base_url + '/add-edit-user/';
var data = {
'email': $(this).data('email'),
'csrfmiddlewaretoken': csrftoken,
};
// $.post(url, data);
$.post(url, data).done(function(result) {
console.log(result);
}).fail(function(error) {
console.log(error);
});
});
The /add-edit-user
URL maps to the following view where I'm trying to define my form object and pass on as a context variable to my template file:
def add_edit_users(request):
form = AddUpdateForm(request.POST or None)
data = {'form': form}
return render(request, 'add_edit_users.html', data)
Now, my assumption was that if the user clicks on the 'edit' button, I'd set the 'email' parameter in a data attribute and pass it on in my POST request to /add-edit-user
URL, after which I'll render my form template with the form object as a context variable which will display the form with email
field already prefilled.
However, on clicking of the edit
link, the page is not redirected to the form page, but I do get the entire form page's HTML in my response. How can I redirect the flow to my form's template so that my input fields are prefilled from the POST variables?
Upvotes: 1
Views: 2218
Reputation: 946
What you want to achieve can be done by creating a form like this:
<form method='post' action='/add-edit-user/'>
{% csrf_token %}
<input type="email" name="email" value="[email protected]" hidden>
<button class="edit-user" type="submit">Edit</button>
</form>
This form sends value of the email field in post data to the url : /add-edit-user/. Resulting in redirecting to the url with the email field prefilled.
Upvotes: 2