I have metronic 8.0.38 I want to implement ajax call to server-side with django for login and registration pupose. How can we implement it?
Hi Nikihil,
Fortunately, Metronic comes with ajax code out of the box.
Firstly, I'd recommend following this tutorial and adjusting the code to your liking.
How to Create Custom Django User Model
Once you've accomplish that you can create a view that only takes in a POST request.
Here is some code from my project that may help you.
views.py
@require_POST
def post(self):
form = YOURUSERFORM(self.POST)
if form.is_valid():
YOURUSERNAMEFIELD = self.POST.get("USERNAME_FIELD_NAME_ON_FRONTEND_FORM")
YOURPASSWORDFIELD = self.POST.get("PASSWORD_FIELD_NAME_ON_FRONTEND_FORM")
response_data = {}
user = USERMODEL(YOURUSERNAMEFIELD=YOURUSERNAMEFIELD, YOURPASSWORDFIELD=YOURPASSWORDFIELD)
user.save()
response_data["result"] = "success" # THIS IS OPTIONAL IF YOU WANT A SUCCESS MESSAGE
return HttpResponse(
json.dumps(response_data),
content_type="application/json"
)
else:
message = form.errors.as_json()
status_code = 400
return HttpResponse(
json.dumps(message),
status=status_code,
content_type="application/json"
)
Hi,
We are about to release the Django starter kit for Metronic in a few days with the main layout, dashboard, and auth pages(sign in, signup, reset the password, and new password).
Regards.
That would be something very interesting.
Thank you