'Is It possible login Django admin via PyQt5?

I'd like to know if it's possible to login to Django via PyQt5. I have been trying a few things but without much success. Below, I try a solution with PyQt5's QtNetwork. This is my code:

import json
import sys

import requests
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QLineEdit, QMessageBox, QLabel
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtCore, QtGui, QtNetwork
from django.test import Client
from querystring import querystring


class AppLogin(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Login in the Desktop Application'
        self.left = 500
        self.top = 300
        self.width = 380
        self.height = 180
        self.username = None
        self.password = None
        self.button = None
        self.nam = QtNetwork.QNetworkAccessManager()

    def __call__(self, *args, **kwargs):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create an username textbox
        self.username = QLineEdit(self)
        self.username.move(20, 20)
        self.username.resize(280, 40)
        self.username.setPlaceholderText('Usuário')

        # Create a password textbox
        self.password = QLineEdit(self)
        self.password.setEchoMode(QLineEdit.Password)
        self.password.move(20, 80)
        self.password.resize(280, 40)
        self.password.setPlaceholderText('Senha')

        # Create a button in the window
        self.button = QPushButton('Login', self)
        self.button.move(20, 140)

        # connect button to function login
        self.button.clicked.connect(self.login)
        self.show()

    @pyqtSlot()
    def login(self):
        # User and passwd in my desktop app
        user = self.username.text()
        passwd = self.password.text()
        
        # My Django app Url 
        url = "http://127.0.0.1:8000/login"
        data = {'username': user, 'password': passwd}

        # Here, I use QtNetWork to takes request
        req = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
        req.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
                      "application/x-www-form-urlencoded")

        self.nam.finished.connect(self.handle_response)
        self.nam.post(req, data)

    # Function to reply error
    def handle_response(self, reply):
        error = reply.error()
        # Doesn't entry here
        if error == QtNetwork.QNetworkReply.NoError:
            print("success!!!")
        
        # Neither entry here 
        else:
            QMessageBox.question(
                self, 'Error', "Unauthenticated user!", QMessageBox.Ok, QMessageBox.Ok
            )
            self.username.setText("")
            self.password.setText("")
        QtCore.QCoreApplication.quit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = AppLogin()
    ex()
    sys.exit(app.exec_())

Explaining better... My problem was creating an application with PyQt5 or Pyside for a job test, which consumed a web authentication service (Django, Flask, etc.). There should be a window in the desktop app for username and password that consults this information. What I sent them was an application that queries my local database, just looking for the username (code here). However, I'd like to know if there is a way following this line of code above.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source