create login authentication and registration with postgresql in django
Installation
creata an virtual environment
$ virtualenv envname
(i hope you know how to activate the environment) if you don't have installed virtual environment in your PC
$ pip install virtualenv
install django in your environment
$ pip install django
or
$ pip3 install django
Create django project
django-admin startproject projcetname
Test
here I'm using authentication as my app check if everything set up nicely!
python manage.py runserver
and open browser go to localhost:8000
create an app inside the project
python manage.py startapp appname
now see you will have a new folder with your app name
then create a urls.py folder inside your app folder for routing
setup views.py for workflow and urls.py(authentication ) for routing
copy the path module from main urls.py form your project and setup route how you want it inside (urlpatterns =[])
from django.urls import path
from . import views
urlpatterns = [
path('', views.index , name='index'),
path('register', views.register, name='register' ),
path('custom',views.custom , name= 'custom'),
path('home',views.home , name= 'home'),]
the below file links your app urls
from django.contrib import admin
from django.urls import path , include
urlpatterns = [
path('',include('authentication.urls')),
path('admin/', admin.site.urls),
for html
to use html in your project make the templates foleder and put all your html file. and set the directory in setting.py file in TEMPLATES section.
'DIRS': [os.path.join(BASE_DIR, 'templates')],
for CSS,js,media
for all these stuff you can make a static folder and put all these here. set the directory in setting.py file in below STATIC_URL variable.
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT = os.path.join(BASE_DIR,'assests')
setting database
In setting.py file in the project directory lookup to DATABASES variable set your engine name, database name, user, password, host and port
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test',
'USER': 'postgres',
'PASSWORD': '8080',
'HOST':'localhost'
}
}
you have to install an adapter to connect your database with your application so we have to install psyopg2
pip install psyopg2
or
pip3 install psycopg2
for login auth:
def index(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username = username, password =password )
if user is not None:
auth.login(request , user)
return redirect('/home')
else:
messages.info(request, 'invalid username or password')
return redirect("/")
else:
return render(request,'index.html')
for registration :
def register(request):
if request.method == 'POST':
email = request.POST['email']
username = request.POST['username']
password= request.POST['password']
user = User.objects.create_user(username = username , password = password , email = email)
user.save()
print('user created')
return redirect('/custom')
return render(request,'register.html')
def custom(request):
return render(request, 'custom.html')
def home(request):
return render(request, 'home.html')
that's all look into authentication folder and template folder for how things work especially views.py and urls.py file. these both are import file for communicating between the application and main projects
Github link https://github.com/cyber-hoax/login_auth_django_postgres
Top comments (0)