Created
May 29, 2023 13:39
-
-
Save muhamedoufi/2dcb1587ac0bed065b204702c4272c05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ser = get_user_model() | |
class SignupForm(UserCreationForm): | |
password1 = forms.CharField(widget=forms.HiddenInput(attrs={'value':'po123456'})) | |
password2 = forms.CharField(widget=forms.HiddenInput(attrs={'value':'po123456'})) | |
# None # Standard django password input | |
# password2 = None # Standard django password confirmation input | |
password = forms.CharField( | |
widget=forms.HiddenInput(attrs={'value': 'po123456'}), # Set the default password | |
) | |
class Meta: | |
model = User | |
fields = ['username', 'first_name', 'last_name', 'email', 'is_admin', 'is_agent','password','password1','password2'] | |
widgets = { | |
'username':forms.TextInput(attrs={'class': 'form-control widget border border-warning rounded'}), | |
'first_name':forms.TextInput(attrs={'class': 'form-control widget border border-warning rounded'}), | |
'last_name':forms.TextInput(attrs={'class': 'form-control widget border border-warning rounded'}), | |
'email':forms.TextInput(attrs={'class': 'form-control widget border border-warning rounded'}), | |
# 'password1':forms.TextInput(attrs={'class': 'form-control widget border border-warning rounded','type':'password'}), | |
# 'password1': forms.HiddenInput(attrs={'value': '123456'}), # Set the default password | |
# 'password2': forms.HiddenInput(attrs={'value': '123456'}), # Set the default password | |
# 'password': forms.HiddenInput(attrs={'value': '123456'}), # Set the default password | |
} | |
def __init__(self, *args, **kwargs): | |
super(SignupForm, self).__init__(*args, **kwargs) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST" action="{%url 'add_utilisateur' %}"> | |
{% csrf_token %} | |
{{create_user_form|crispy}} | |
<div class="modal-footer "> | |
<button type="submit" class="btn" | |
style="background-color:#FF8C00; border: none; color: white;">Ajouter</button> | |
<button type="reset" class="btn" style="color:#FBA214;" data-dismiss="modal">Annuler</button> | |
</div> | |
</div> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UtilisateurCreateView(CreateView): | |
model = User | |
form_class = SignupForm | |
template_name = 'mainapp/tables/parametre.html' | |
success_url = reverse_lazy('employee-list') | |
def form_valid(self, form): | |
user = form.save(commit=False) | |
user.save() | |
messages.success(self.request, "L'utilisateur a été ajouté avec succès.") | |
return super().form_valid(form) | |
def form_invalid(self, form): | |
error_message = "" | |
for field, errors in form.errors.items(): | |
error_message += f"<br /> - {field}: {', '.join(errors)}" | |
messages.error(self.request, error_message) | |
return HttpResponseRedirect(reverse_lazy('parametre')) | |
def get_success_url(self): | |
return reverse('parametre') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment