Hello,
This is my first project with Metronic and Django, and it's so hard...
I try to implement signin with email and password. My model is ok, but my form doesn't run.
Someone would be kind show me a view and un template "signin" running ?
Thanks a lot.
url.py
urlpatterns = [
#path("", AuthSigninView.as_view(template_name = "pages/auth/signin.html"), name="signin"),
path("", LoginPageView.as_view(template_name = "pages/auth/signin2.html"), name="signin"),
from django.views.generic import TemplateView
from django.conf import settings
from _keenthemes.__init__ import KTLayout
from _keenthemes.libs.theme import KTTheme
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect, render
from auth import forms
"""Connexion à la plateforme"""
class LoginPageView(TemplateView):
template_name = "pages/auth/signin2.html"
form_class = forms.LoginForm
def get(self, request):
form = self.form_class()
message = ""
return render(request, self.template_name, context={"form": form, "message": message})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = authenticate(
email=form.cleaned_data["email"],
password=form.cleaned_data["password"],
)
if user is not None:
login(request, user)
return redirect("home")
message = "Identifiants invalides."
return render(request, self.template_name, context={"form": form, "message": message})
{% extends layout %}
{% block title %}Identification{% endblock %}
{% block content %}
<form method="post">
{{ form.as_p }}
{% csrf_token %}
<button type="submit" >Se connecter</button>
</form>
{% endblock content %}
Hi,
You can check the Django documentation for customization and more advanced integration with the Django backend. In Metronic, we have prepared and focused on front-end integrations.
https://docs.djangoproject.com/en/4.1/topics/forms/
Thanks
Hello,
Thanks you for your answer, but i don't know how to integrate "POST" function :
<img>
from django.views.generic import TemplateView
from django.conf import settings
from _keenthemes.__init__ import KTLayout
from _keenthemes.libs.theme import KTTheme
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect, render
from django.http import *
class AuthSigninView(TemplateView):
template_name = "pages/auth/signin.html"
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# A function to init the global layout. It is defined in _keenthemes/__init__.py file
context = KTLayout.init(context)
KTTheme.addJavascriptFile("js/custom/authentication/sign-in/general.js")
# Define the layout for this module
# _templates/layout/auth.html
context.update({
"layout": KTTheme.setLayout("auth.html", context),
})
return context
def post(self, request, **kwargs):
email = request.POST.get("email", False)
password = request.POST.get("password", False)
user = authenticate(email=email, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("dashboard")
return render(request, self.template_name)
Hi Romain TALDU
The var "layout" is an extension of the main layout.
context.update({
"layout": KTTheme.setLayout("auth.html", context),
})