'How to add Icons in Django forms creating forms from Models
i am newbie in python, i'm try to create forms from models in django. I need to put icons in the html5 lines
i'm using fontawesome and i can put icon if i add the html codeline but i need to bring the variable "fa fa-shower" or "{icon name}" from my forms.py
my code in forms.py is:
class LoginForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'password']
icons = {
'username': 'fa fa-user',
'password': 'fa fa-lock',
}
labels = {
'username': 'User/Correo: ',
'password': 'Contraseña ',
}
widgets = {
'username': forms.TextInput(attrs={'class': 'form-control'}),
'email': forms.TextInput(attrs={'class': 'form-control'}),
'password': forms.PasswordInput(attrs={'class': 'form-control'}),
}
class iconModelForm(LoginForm):
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
icons = getattr(self.Meta, 'icons', dict())
for field_name, field in self.fields.items():
if field_name in icons:
field.icon = icons[field_name]
how can pass the context of two classes from view to html?
my views.py is:
class LoginLocal(View):
def get(self, request):
contexto = {
'formLogin': LoginForm(),
# 'formIcon': iconModelForm(),
}
print(contexto)
return render(request, 'login_reg_app/login.html', contexto)
and how can i read two list in the same for in html? I tried to use ZIP but it showed error.
my file login.html is:
<form action="{% url 'login_reg_app:login' %}" method="post" >
{% csrf_token %}
{{ formLogin.non_field_errors }}
{% for field in formLogin %}
<div class="mt-3 row">
<label for="{{ field.id_for_label }}" class="col-sm-3 col-form-label"><i class="{{ field.icon }}"></i>{{ field.label }}</label>
<div class="col-sm-9">
{{ field }}
</div>
{% if field.errors %}
<div class="alert alert-danger mt-2" role="alert">
{% for error in field.errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
i want to know, how can i read the second context in <i class="{{ field.icon }}"></i> using my class iconModelForm?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
