BigBerger
BigBerger

Reputation: 1803

Using a database in a Django view

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.

Upvotes: 1

Views: 49

Answers (1)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83358

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

Upvotes: 2

Related Questions