I tried to make a ModelForm for changing the email in the user model but the input field on the page wont appear. I guess i messed something up in the views.py
views.py
@login_required(login_url='home:login')
def ChangeEmailView(request):
if request.method == 'POST':
form = EmailChangingForm(request.POST)
if form.is_valid():
emailvalue = form.save()
account = User.objects.update(user=request.user, email=emailvalue)
account.save()
return redirect('home:profilesettings')
else:
form = EmailChangingForm()
return render(request, 'home/email_settings.html')
forms.py
from django.contrib.auth import forms
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, UserCreationForm
from django.contrib.auth.models import User
from django.contrib.auth.views import PasswordResetConfirmView, PasswordResetView
from django.forms import ModelForm
from django.forms.fields import CharField, EmailField
from StartSite.models import Account
from django.contrib.auth.forms import PasswordChangeForm
from django.forms.widgets import EmailInput, PasswordInput
class EmailChangingForm(ModelForm):
class Meta:
model = User
fields = ['email']
...
template
<form method="post" class="id_password_reset_form">
{% csrf_token %}
<p>Please enter your new Email</p>
<h6 class="mb-0">New Email</h6>
<div class="pw_space2"></div>
{{ form }}
<div class="pw_space2"></div>
<button style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " type="submit">Change Email</button>
<a style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " target="__blank" href="{% url 'home:profilesettings' %}">Cancel</a>
</form>
urls.py
from django.contrib.auth.forms import PasswordChangeForm
from django.urls import path
from django.contrib.auth import views as auth_views
from django.urls.base import reverse_lazy
from . import views
app_name = 'home'
urlpatterns = [
path('', views.index_view, name="index"),
path('login/', views.login_view, name="login"),
path('register/', views.register_view, name="register"),
path('logout/', views.logout_view, name="logout"),
path('profile/', views.profile_view, name="profile"),
path('settings/', views.profilesettings_view, name="profilesettings"),
path('settings/prename/', views.AccountInfoPrename, name="ps_prename"),
path('settings/company/', views.AccountInfoCompany, name="ps_company"),
path('settings/country/', views.AccountInfoCountry, name="ps_country"),
path('settings/surname/', views.AccountInfoSurname, name="ps_surname"),
path('settings/email/', views.email_settings_view, name="emailsettings"),
path('settings/language/', views.language_settings_view, name="languagesettings"),
path('plans/', views.plans_view, name="plans"),
path('settings/password/', views.PasswordChangeView.as_view(success_url=reverse_lazy('home:password_success'), template_name='home/password_settings.html'), name="passwordsettings"),
path('settings/password_success/', views.password_success, name="password_success"),
path('converter/', views.vc_view, name="vc"),
path('imageupload/', views.imgupload_view, name="imgupload"),
]