Python Forum
Post HTML Form Data to API Endpoints
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post HTML Form Data to API Endpoints
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to scrape data from HTML with no identifiers pythonpaul32 2 836 Dec-02-2023, 03:42 AM
Last Post: pythonpaul32
  Post Python to HTML/CSS Extra 3 1,885 Feb-17-2022, 06:07 PM
Last Post: snippsat
  POST Reply to Ajax/jQuery (mostly an HTML question) gw1500se 5 2,673 Nov-18-2021, 02:44 PM
Last Post: gw1500se
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,614 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Cleaning HTML data using Jupyter Notebook jacob1986 7 4,119 Mar-05-2021, 10:44 PM
Last Post: snippsat
  Using Python request without selenium on html form with javascript onclick submit but eraosa 0 3,168 Jan-09-2021, 06:08 PM
Last Post: eraosa
  Any way to remove HTML tags from scraped data? (I want text only) SeBz2020uk 1 3,452 Nov-02-2020, 08:12 PM
Last Post: Larz60+
  Using python within an html form t4keheart 5 5,391 Aug-17-2020, 12:28 PM
Last Post: t4keheart
  POST request with form data issue web scraping hoff1022 1 2,673 Aug-14-2020, 10:25 AM
Last Post: kashcode
  html data cell attribute issue delahug 5 3,135 May-31-2020, 09:18 AM
Last Post: delahug

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020