Python Forum
Post HTML Form Data to API Endpoints - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Post HTML Form Data to API Endpoints (/thread-35517.html)



Post HTML Form Data to API Endpoints - Dexty - Nov-11-2021

I'll like to know the suitable method for posting form data to DRF API endpoints. The idea is to post and query the API's model. A team member has already made the API and it authenticates users too. I'm to link the frontend to the API endpoints accordingly and display the response in the appropriate templates. Here's my approach:

index.html
<form method="post" action="{% url 'dashboard' %}">

{% csrf_token %}

<!-- Login Form with username and password fields and submit button-->

</form>
views.py

from django.shortcuts import render
import requests

def dashboard(request):
    req = requests.post('https://loginEndpoints.com/api/password_token/', params=request.POST)
    showTxt = req.text 
    print(req.status_code)
    print(req)                    
    return render(request, 'accounts/dashboard.html', {'req': req, 'showTxt': showTxt})
This takes users straight to dashboard.html.

The challenge is getting the users' credentials from index.html and posting it via requests in dashboard() in views.py. If this goes well, we can direct users to the template tailored for their specific role (the dashboard.html having varying functionalities depending on the role).
I should also add that I don't have a models.py as I'm to rely on the API's.