I'm learning python and I want to understand the database section and when setting up for postgresql database.
https://docs.djangoproject.com/en/1.9/ref/settings/#databases
Are all the values necessary?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Specifically USER
, PASSWORD
, HOST
, PORT
? Is USER
and PASSWORD
values that we can create in django settings.py? Or is this the actual USER/PASSWORD
of the database? Also, HOST is currently 127.0.0.1
for localhost, but when deploying to production, do I change this to the domain name (http://www.example.com)? And PORT
, is it necessary?
from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...
auth import authenticate # Authenticate user = authenticate(username=username, password=password) if user is None: return HttpResponseForbidden() else: # Ready for operation ... Save this answer.
The correct way to create a user in Django is to use the create_user function. This will handle the hashing of the password, etc..
The settings.py file contains all the project settings along with database connection details. By default, Django works with SQLite, database and allows configuring for other databases as well. Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc.
Yes! all of those information are necessary , there is no way you could connect to the database unless those values are specified.
Yes! user
and password
are the actual credentials of your PostgreSQL database.
regarding deployment, you should set the correct IP/host where your production database is located. that might be example.com or xxx.xxx.xxx.xxx
I think you are concerned about security
(revealing your database credentials in the source) if that is the case you could put your credentials in secure config file like .env and use this library to work with your config file.
Specifically USER, PASSWORD, HOST, PORT? Is USER and PASSWORD values that we can create in django settings.py? Or is this the actual USER/PASSWORD of the database? Also, HOST is currently 127.0.0.1 for localhost, but when deploying to production, do I change this to the domain name (http://www.example.com)? And PORT, is it necessary?
The USER
and PASSWORD
is what you configure in the database, then you enter it in the file.
The HOST
is the IP address or hostname where the server is running. In production, you have to check with your hosting provider for the correct details; it is rare that it is your domain name.
The PORT
you only need to adjust if its different than the default port (5432
). If it is different, your host will tell you.
Finally, keep in mind that http://www.example.com
is not a domain name, this the the complete URL. The domain name is example.com
, and the host is www
, the fully qualified hostname is www.example.com
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With