my form.py
from app.models import CredentialModels
class EmailForm(forms.Form):
email = forms.ChoiceField(choices=[], widget=forms.Select(attrs={'class': 'form-control'}))
signature = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 5}))
def __init__(self, user_id, *args, **kwargs):
super().__init__(*args, **kwargs)
credentials = CredentialModels.objects.filter(user_id=user_id)
self.fields['email'].choices = [(c.email, c.email) for c in credentials]
My views.py:
def update_signature(request):
email_form = EmailForm(user_id=request.user.id)
return render(request, 'update_signature.html', {'email_form': email_form})
My model:
class CredentialModels(models.Model):
user_id = models.CharField(max_length=255)
email = models.EmailField()
access_token = models.CharField(max_length=255)
token_expiry = models.DateTimeField()
refresh_token = models.CharField(max_length=255)
client_id = models.CharField(max_length=255)
client_secret= models.CharField(max_length=255)
def __str__(self):
return self.email
class Meta:
verbose_name_plural = "Email"
And form:
{% block content %}
<div class="row">
<div class="col-md-6 offset-md-3">
<form method="get">
{% csrf_token %}
<div class="form-group">
{{ email_form.email.label_tag }}
{{ email_form.email }}
</div>
<div class="form-group">
{{ email_form.signature.label_tag }}
{{ email_form.signature }}
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
{% endblock %}
I want to see the email of the current loggedin user on form select. But it shows empty dropdownlist