python - Django Rest Framework -


i have method grabs post data formated in json format this

[{"username": "alexgv", "password": "secretpassword"}] 

here method

def login(request, *args):     data = request.data     return response(data) """ try:     m = user.objects.get(username=request.data['username'])     if m.password == request.data['password']:         request.session['member_id'] = m.id         return httpresponse("testing") except user.doesnotexist:     return httpresponse("your username , password didn't match.") """  

i want able take 1 variable json post. example, maybe want grab username or password. how that? have tried variety of things cant seem work, , dont want use request.post.get because means have send post variables. btw using http://django-rest-framework.org/. have read through docs cant seem find in there. appreciated. returns right everything.

like so...

username = request.data['username'] 

incidentally, shouldn't writing session based api login views it's easy wrong.

for apis provide ajax style functionality have 2 options:

  • login using standard django login, performed user, not performed api client.
  • use credentials based authentication scheme, rather session based, , perform login using ajax. example djoser third party package great library including token-based login , other similar views... https://github.com/sunscrapers/djoser

update discovered https://github.com/jamesritchie/django-rest-framework-sav might worth ajax session based authentication.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -