0

I have a form class where I ask for a username and password. I created my own authenticate function. I simply want to pass the username and password input to the authenticate function. I've tried saving it as self.user/self.pw or passing it directly.

Thanks in advance!

I am using Django lockdown. I don't need to save the username and password to a database because I'm using my own authentication function.

class loginform(forms.Form):

    username = forms.CharField ...
    password = forms.CharField... 


    def authenticate(self, request, username, password):
        print("this actually prints")
        '''authenticate with passed username and password'''
furas
  • 134,197
  • 12
  • 106
  • 148
asor
  • 1
  • 1

1 Answers1

0

You can access them through cleaned_data attribute of the class. For example:

class loginform(forms.Form):  # Please use PascalCase when defining class name(as per pep-8 style guide)
    # rest of the code...

    def authenticate(self, request):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • Error says loginform object has no attribute 'cleaned_data' :( – asor Jul 30 '19 at 04:35
  • you need to call [`is_valid`](https://docs.djangoproject.com/en/2.2/ref/forms/api/#django.forms.Form.is_valid) method of the form to get the cleaned_data – ruddra Jul 30 '19 at 04:37
  • Is there a way to do that inside the form file? I know how to do that in views but can't seem do it specifically for django-lockdown – asor Jul 30 '19 at 04:39
  • When I call is_valid, it does something with main project's form which is different from this loginform. It says "required" for specific models as if my main project's form has been submitted. I'm not sure how to undo this. The login credentials are required after any submit or url change instead of being required after a browser session ends. Sorry for the long paragraph – asor Jul 30 '19 at 05:28
  • sorry, I am not getting what you meant. What do you mean by main project's form? Also, is_valid should be called only for `loginform` and it will not affect any other forms unless you call their `is_valid` method – ruddra Jul 30 '19 at 05:41
  • So my Django project is a form-like page filled with models with a submit button at the end. My loginform is in a forms.py and grants access to the models in my views.py for the entire project. When the login button is submitted with the correct credentials, it goes to the correct page but it's almost as if the submit button on my project page was clicked. It requires my login credentials to press submit again or to go to another page even though I just logged in. – asor Jul 30 '19 at 05:47
  • IMHO, i don't think its related to the code which you have shared with this question. TBH, I am not clear what is the problem without seeing the rest of the codes. Maybe you can ask a new question with the problem, and we can see why its occuring – ruddra Jul 30 '19 at 05:52