Introducing ReUI:Open-source UI components and apps built with React, Next.js and Tailwind CSS
Browse ReUI

Metronic Signin whith Django


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"),


view.py

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})


signup2.html

{% 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 %}


<b>Error : </b>
TemplateSyntaxError at /
Invalid template name in 'extends' tag: ''. Got this from the 'layout' variable.
Request Method:GET
Request URL:http://127.0.0.1:8000/
Django Version:4.1
Exception Type:TemplateSyntaxError
Exception Value:
Invalid template name in 'extends' tag: ''. Got this from the 'layout' variable.
Exception Location:C:\www\python\venv\lib\site-packages\django\template\loader_tags.py, line 122, in get_parent
Raised during:auth.signin.views.LoginPageView


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


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),
})


Please add the above code in the local view.py.
starterkit/auth/signin/views.py

Thanks


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(