Django google-authentication while using XAMPP(mysql) - django

I Am working on a Django project, and now I want to add google authentication To my project.
when I'm adding Authentication: I face the redirect page problem.
task/problem: we need to redirect the home page with some MySQL data(XAMPP).
Error: after migration, I run the manage.py runserver.
when I'm opening the localhost:8000/account/login, it's now working.
if I change in setting.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'test1',
# 'HOST': 'localhost',
# 'USER': 'root',
# 'PASSWORD': '',
# 'PORT': '3306',
# 'OPTIONS':{
# 'init_command':"SETsql_mode='STRICT_TRANS_TABLES'",
# }
}
}
after the authentication, it's showing:
error type: Reverse for 'home' not found. 'home' is not a valid view function or pattern name.

Related

Problem with database when I use .env file

When I try to migrate some changes on one of the models I get this error but only when I use .env file in settings.py:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
My settings.py
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}
My .env file
# DB
DB_ENGINE=django.db.backends.postgresql
DB_NAME=db
DB_USER=user
DB_PASSWORD=password
DB_HOST=127.0.0.1
DB_PORT=5432
My Pycharm .env settings
When I set up my settings.py back to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
I don't have any problems to migrate.
You should load the .env file inside your settings, not PyCharm.
One way to do this is to use django-environ.
Here's how you would use it in your settings.py:
import environ
import os
env = environ.Env()
# Set the project base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# Load the environment variables
DATABASES = {
'default': {
'ENGINE': env('DB_ENGINE'),
'NAME': env('DB_NAME'),
'USER': env('DB_USER'),
'PASSWORD': env('DB_PASSWORD'),
'HOST': env('DB_HOST'),
'PORT': env('DB_PORT'),
}
}
Be sure to install the package first.
Check out the quick start or this Medium article for more examples.

Cannot connect to "#localhost". Access denied for user 'root'#'localhost' (using password: YES)

I try to connect mysql to django project
Here my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': 3306,
}
}
enter image description here
Looks like you do not have such user in your database.
Please use that command in terminal, while being inside of your app's main folder:
python manage.py createsuperuser

i am getting error while giving my ip and localhost name in postgres admin

I am getting error while giving my IP address and localhost name in PostgreSQL admin, any help would be appreciated.
You can take a look at my PostgreSQL admin image here:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'fakepassword',
'HOST': 'localhost',
'PORT': '',
}
}

Django - Adding a second database entry causes failure. Django no longer uses 'default'

My database and project work great using this database setting:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
}
But I want to add a 'readonly' database entry for my readonly db user, like this, so that I can run django-sql-explorer:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
'readonly': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectreadonly',
'PASSWORD': 'your_eyes_only_secret',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
And now django throws a couple different errors. If I try to do anything with migrations, I get:
django.db.utils.ProgrammingError: permission denied for relation django_migrations
If I try to runserver, I get:
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.User' that has not been installed
It seems like when I add the second database setting, django is attempting to use that for migrations, running the server, etc, even though it's not 'default'
If I comment out the second entry, everything works great again.
Any recommendations on how to correct this?
Edit: If I change the settings to use the same username and password, everything works great, so it's not just an issue of django being confused on names. IE:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
'readonly': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
The two configurations shares the same database names 'NAME': 'myprojectdb'
So when you try to migrates dbname seems to conflicts so the applications is having an indecision on which to use The previous or the later

How to add a remote postgres database in my django app?

What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'snackvoxadmin'
},
'users': {
.....
}
}
The database user has a url like similar to this one: postgres://a78adj1he81....
You can decompose your database url and configure it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
And the pattern for a database url is :
postgres://user:password#host:post/database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases
Or you can use the package dj-database-url to directly use the database url.
E.g. from readme :
import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.