Django signup

 Description

 We are building the customized application for user management for signup


Signup

In settings.py

INSTALLED_APPS = [
'polls.apps.PollsConfig',
'user_mgmt.apps.UserMgmtConfig',

In Master urls.py


from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('polls/', include('polls.urls')),
path('user_mgmt/', include('user_mgmt.urls')),
path('admin/', admin.site.urls),

]

In app urls.py


from django.urls import path

from . import views

app_name = 'user_mgmt'
urlpatterns = [
path('signup', views.signup, name='signup'),
]

In views.py

from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
# Create your views here.


def signup(request):
page = 'signup'
form = UserCreationForm()
return render(request, 'user_mgmt/userMgmtForms.html', {'page': page, 'form': form})

 
Here we are not handling post action. Its just to show case the static form.
We will handle the post actions in next steps

 

In templates/userMgmtForms.html

 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ page }}</title>
</head>
<body>
{% block content %}
{% if page == 'signup' %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
{% endif %}
{% endblock content %}
</body>
</html>
 
 
 
 
if we click the signup button, it will not do any direction because we dont have any post action

In Views.py


from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
# Create your views here.


def signup(request):
page = 'signup'
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'User created successfully')
else:
form = UserCreationForm()
return render(request, 'user_mgmt/userMgmtForms.html', {'page': page, 'form': form})
 
 

 In Templates/html


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ page }}</title>
</head>
<body>
{% block content %}
{% if page == 'signup' %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
{% endif %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock content %}
</body>
</html>
 
 


In the backend

 
 















Comments

Popular posts from this blog

Django Server setup with Apache with mod_wsgi in Centos 9

Mysql Installation on CentOS 9

Django polls app with basics of views, url, admin and templates