Django forms styling input field for login page - django

I am working with Django templets and I am trying to create a beautiful login page.
I want to use this snippet to beautify my login page input fields.
<div class="form-label-group">
<input type="username" id="inputuser" class="form-control" placeholder="Email address" required autofocus>
<label for="inputuser">Username</label>
</div>
<div class="form-label-group">
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<label for="inputPassword">Password</label>
</div>
</div>
where should I keep my {{form.username}} and {{form.password}} in the above snippet
The input fields UI should look like :
But my UI looks like :
my urls.py has :
path('login/', auth_views.LoginView.as_view(template_name='post/login.html'), name='login'),
Where should i add the form fields so that i can get the desired UI for my input fields

The {{form.username}} will go inplace of input type="username" and {{form.password}} inplace of input type password this will create default email boxes.
If you want to use your own UI just make sure, the input html element has corresponding name="username" and name="password" as an attribute.
<div class="form-label-group">
<input type="username" id="inputuser" name="username" class="form-control" placeholder="Email address" required autofocus>
<label for="inputuser">Username</label>
</div>
<div class="form-label-group">
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<label for="inputPassword">Password</label>
</div>
</div>

By using django-widget-tweaks I was able to create this beautiful login page.
We just need to add the class name to the render_field
Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes are supported.
That should be enough for designers to customize field presentation (using CSS and unobtrusive javascript) without touching the python code.
The changes which I made are:
{% load widget_tweaks %}
<div class="form-label-group">
{% render_field form.username placeholder=form.username.label class+="form-control" %}
<label for="id_username">User name</label>
</div>
<div class="form-label-group">
{% render_field form.password placeholder=form.password.label class+="form-control" %}
<label for="id_password">Password</label>
</div>

widget_tweak is an excellent solution. Here are the steps to install.
pip install django-widget-tweaks.
add 'widget_tweaks' to APPLICATION_INSTALLED in settings.py.
use in template.
{% load widget_tweaks %}

Related

"placeholder-shown" selector not working with django forms

I have input fields that lower label when something has been typed in it.
paste bin css
<div class="form__group">
<input type="text" class="form__input" id="name" placeholder="Full name" required=""
autocomplete='off' />
<label for="name" class="form__label">Full Name</label>
</div>
But, when I use django forms, they don't work at all
<div class="form__group col">
{{createSet.title}}
<label for="id_title" class="form__label">Title</label>
</div>

How to customize login form design?

I want to customize and create my own design on my login form but i don't know how to call the login form i use the form inside the admin. How can i remove the username: & password: and put it inside the text box ?
i don't know how to start.
Login.html
<div class="container">
<form method="post">
{% block content %}
{% csrf_token %}
{{ form.as_p }}
<button type="submit" name="button">Login</button>
{% endblock %}
</form>
</div>
{% endblock %}
my output
Username: {text box}
Password: {text box}
login button
what i want is
{text box "Username" "username_icon"}
{text box "Password" "password_icon"}
button login
You can write custom html for username and password field
<div class="form-group has-feedback">
<label for="username"></label>
<!-- your icon -->
<input type="text" id="username" class="form-control" name="username" placeholder="Username" required="">
</div>
<div class="form-group has-feedback">
<label for="password"></label>
<!-- your icon -->
<input type="password" id="password" class="form-control" name="password" placeholder="Password" required="">

Django allauth override default template with custom template

I'm currently trying to override a Django allauth template. So far, I followed all the recommendations on similar questions to mine:
overriding default templates of django-allauth
How to override template in django-allauth?
However, my goal is just change the default Django allauth without making new views or forms. (In previous projects I used to override the default forms constructor classes with the helper of Django Crispy Forms which is not possible in this particular case.)
I believe there is a way to ignore the {{form}} variable and just define a working form from scratch. Currently, I have my template being successfully being displayed when I navigate to 'account_login'. However, when I press Sign In nothing happens.
Here is my myapp/templates/account/login.html file:
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}
Sign In to {{ SITE_NAME }}
{% endblock %}
{% block body %}
<div class="centered">
<img class="logo" src={% static 'imgs/logo.png' %} alt="" width="100" height="100">
<form id="login_form" class="form-signin" method="post" action="{% url 'account_login'%}">
{% csrf_token %}
<label for="inputLogin" class="sr-only">Email address or Username</label>
<input type="text" id="login" class="form-control" placeholder="Email or Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="password" class="form-control" placeholder="Password" required>
<div class="note mb-3">
<label>
<input type="checkbox" value="remember" id="remember"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<div class="note mb-3 text-center">
<p>Forgot? Retrieve your login.</p>
<p>Not registered? <a href={% url 'account_signup'%}>Sign Up!</a></p>
</div>
</form>
</div>
{% endblock %}
Yes, you can customize it the way you want. It is rather simple.
When you use {{ form.as_p }} the page renders the form. And if you inspect the form you will notice that the page rendered the following form:
notice the id="" and the name="" you have to name it the exact same.
With that your code should look like this:
<form id="login_form" class="form-signin" method="post" action="{% url 'account_login'%}">
{% csrf_token %}
<label for="inputLogin" class="sr-only">Email address or Username</label>
<input type="text" name="login" id="id_login" class="form-control" placeholder="Email or Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="id_password" class="form-control" placeholder="Password" required>
<div class="note mb-3">
<label>
<input type="checkbox" name="remember" value="remember" id="id_remember"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<div class="note mb-3 text-center">
<p>Forgot? Retrieve your login.</p>
<p>Not registered? <a href={% url 'account_signup'%}>Sign Up!</a></p>
</div>
</form>
And you should be good to go.

How to Call a Django Rest Api from the form action method

I want to upload a csv file to the database using Django rest api.
My content of html page
<form class="form-inline " method="POST" action="uploadfile" enctype="multipart/form-data">
<div class="form-group col-lg-3 col-sm-5">
<label for="selectclient">SELECT CLIENT : </label>
<select class="form-control ml-2 w-50" id="selectclient">
<option>Client</option>
<option>USPLIMARA</option>
<option>USPLMSTAKEN</option>
<option>USPLWROGN</option>
</select>
</div>
<div class="form-group col-lg-3 col-sm-3">
<label for="selecttype">Type : </label>
<select class="form-control ml-2 w-50" id="selecttype">
<option>Type</option>
<option>SALES</option>
<option>INVENTORY</option>
<option>RETURNS</option>
</select>
</div>
<div class="form-group col-lg-3 col-sm-4 ">
<label for="selectdburl">DBURL : </label>
<select class="form-control w-50 ml-2" id="selectdburl" name="dburl">
<option>Dburl</option>
<option>localhost:3306/usplimara</option>
<option>localhost:3306/usplmstaken</option>
<option>localhost:3306/usplwrogn</option>
</select>
</div>
<div class="form-group col-lg-2 col-sm-4 ">
<input type="file" id="uploadfile1" name="missingcsvfile" class="mt-2">
<button type="submit" class="btn btn-outline-primary btn-sm mt-2">Upload file</button>
</div>
</form>
I created a Rest Api
urlpatterns = [url(r'^upload/(?P<filename>[^/]+)$', views.FileUploadView.as_view()),]
I don't know how to call the api to access this Api or how to specify it in the action method. Please let me know.
Action attribute tells where the data from the form should be send. One tricky thing, is that you have an URL parameter with a name of the file defined in the URL configuration. So it'd have to be different for every new file. If it has to stay this way, you'd have to use JavaScript to dynamically override action attribute based on the name of the file.
Another solution would be to change your URL configuration so you can upload using /upload without the need of specifying the name in the URL (you can always send the name as a part of the form data)

Can't get single checkbox to render in Flask WTF

I've been searching a long time for examples on how to use checkboxes with Flask WTF, but it seems like either there are no example or there are really complex ones that involve a list of checkboxes.
I did find one example in Miguel's blog here, but I'm not able to get the checkbox to render. Here's what I did:
First off, the form class:
class LoginForm(Form):
email = TextField("Email address", validators=[validators.required()])
password = PasswordField("Password", validators=[validators.required()])
remember_me = BooleanField("Remember me", default = False)
submit = SubmitField("Login")
And then in my template:
<form class="form-signin" role="form" action="{{ url_for('login') }}" method="post">
{{ form.hidden_tag() }}
<div class="form-group">
{{ form.email.label }}
{{ form.email ( ** {'class' : 'form-control'} ) }}
</div>
<div class="form-group">
{{ form.password.label }}
{{ form.password ( ** {'class' : 'form-control'} ) }}
</div>
<div class="form-group">
{{ form.remember_me }} Remember me
</div>
</form>
In the final output of the form, the checkbox is missing:
If I see the generated HTML, the field is just missing:
<form class="form-signin" role="form" action="/login" method="post">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1457326344##3036eeab4ffa39494ee92247925a30a88a69432f"><input id="next" name="next" type="hidden" value=""></div>
<div class="form-group">
<label for="email">Email Address</label>
<input class="form-control" id="email" name="email" type="text" value="">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" id="password" name="password" type="password" value="">
</div>
<div class="form-group">
Remember me
</div>
<div class="form-group">
<input class="btn btn-primary btn-lg" id="submit" name="submit" type="submit" value="Login">
</div>
</form>
Curiously, if I try to add {{ form.remember_me.label }} to the template, I get an exception that jinja2.exceptions.UndefinedError: 'flask_security.forms.LoginForm object' has no attribute 'remember_me'.
I also came across some solutions that mention custom widgets, but I'm really hoping to hear I won't have to go that route for a simple, single checkbox.
What am I doing wrong? If nothing, what's the shortest, most painless way to accomplish this?
~~~~Update~~~~
It looks like there's some weird behavior at work here. The field form.remember seems to produce a checkbox, and form.remember.label produces a default "Remember Me" label. Using any other name causes the aforesaid Exception to occur.
I eventually found my mistake. The import from security import * caused a flask-security template LoginForm to creep into my namespace, causing the weird behavior. I also needed to make some changes to my forms, importing the Form from flask.ext.wtf but the rest of the fields from wtforms. Hope it helps somebody!