How to implement a user login in Python with Kivy and Parse REST API

I am trying to create a new account for a new user and have them log in successfully for an app. I am programming it in Python, using Kivy framework and Parse API for the backend. It works if I do it like the example on the Parse website when the username and password are hard coded in, however when I try to take the user's input from a text field, it is not connecting. I just don't understand how to get the user's input for username and password in there where it works.

Below is my code in kv and in python. Sorry about the format...I'm not sure why it's behaving this way when I post it.

kv code

<SignUp>:
   _username: _username
   _password: _password
   FloatLayout:
      Label:
         text: 'Create a new account'
         pos: 350, 450
         size_hint: .1, .2
         font_size: 36
      TextInput:
         id: _username
         hint_text: 'Username'
         font_size: 20
         pos: 280, 350
         size_hint: .3, .07
      TextInput:
         id: _password
         hint_text: 'Password'
         font_size: 20
         pos: 280, 300
         size_hint: .3, .07
      Button:
         text: 'Create Account'
         pos: 280, 200
         size_hint: .3, .07
         font_size: 16
         on_press: root.createUserAccount()

Python code

class SignUp(Screen):
   def __init__(self, **kwargs):
      super(SignUp, self).__init__(**kwargs)


   def createUserAccount(self):
      _username = ObjectProperty(None)
      _password = ObjectProperty(None)

      try:
         connection = httplib.HTTPSConnection('api.parse.com', 443)
         connection.connect()
         connection.request('POST', '/1/users', json.dumps({
            "username": self._username,
            "password": self._password,
         }), {
            "X-Parse-Application-Id": "nfgytgRuqQwkOqHxEhOEHKisT4sAxFIbCoOvbR5q",
            "X-Parse-REST-API-Key": "j9Qm7b6TuKZFiIAbONytGSWDLAAvWaie0dokk5nE",
            "Content-Type": "application/json"
         })
         result = json.loads(connection.getresponse().read())
         print result
      except:
         print "Error: Unable to connect!"

Answers


def createUserAccount(self):
    _username = ObjectProperty(None)
    _password = ObjectProperty(None)

This syntax is wrong. You should declare properties at class level (the same level as the def). The code still runs because you don't actually rely on these values, and because your references in the kv code automatically create the properties at class level for you.

        "username": self._username,
        "password": self._password,

self._username is a TextInput, as is self._password. You don't want to send these off to the server, that wouldn't make sense - rather, just the text property of each of them.

        "username": self._username.text,
        "password": self._password.text,

I suggest playing around with print statements and/or a debugger to help understand exactly what each of these objects refers to. This would also have caught the original problem for you - that you weren't sending the right data.


Need Your Help

Use data attribute as post data in .load

jquery html5-data

Can I use a data attribute to pass a variable into jquery .load?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.