1

I am wondering how I can update and access django database information while I am inside of a view.

For example, if I was given an HttpRequest inside a view that passed a username and password parameter, and I had already set up a User model with a database in my 'framework' project, how would I go about checking my 'framework_users' database for that username and password?

Thank you in advance for any help you might give.

1 Answer 1

2

In Django you access database via models.

First you configure your database access and credentials in settings.py:

https://docs.djangoproject.com/en/dev/ref/settings/#databases

Then you can perform queries against your models:

https://docs.djangoproject.com/en/dev/topics/db/queries/

E.g.

User.objects.get(username=request.POST["username"])

Then there is a special case for safe user authentication for which Django has its own procedure:

https://docs.djangoproject.com/en/dev/topics/auth/default/#auth-web-requests

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help! Oddly enough I just found out about this feature right before I saw that you posted it. But thank you for the post none the less! On the Django safe user authentication, I am not going to use this because my application is based upon receiving Http Requests, performing tasks based on those requests, and it will only do these tasks if a valid username and password is passed. But thank you for the advice.
You still should use authenticate() to check if the password is valid. It provides some forward-compatibility for different password encryption and such. Do not do manual database look ups or try to verify the password yourself if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.