1. Update forms.py as I add ProfileCreationForm:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password
UserModel = get_user_model()
class MyHolidayUserCreationForm(UserCreationForm):
user = None
def clean_password2(self):
password2 = super().clean_password2()
validate_password(password2)
return password2
class Meta(UserCreationForm.Meta):
model = UserModel
fields = ('email',)
class MyHolidayUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = UserModel
class ProfileCreationForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'age', 'profile_photo']
2. Views will be:
from django.contrib.auth import views as auth_views, login, logout
from django.contrib.auth.mixins import AccessMixin
from django.shortcuts import redirect, get_object_or_404, render
from django.urls import reverse_lazy, reverse
from my_holiday.accounts.forms import MyHolidayUserCreationForm, ProfileCreationForm
from my_holiday.accounts.models import Profile
from django.views.generic import DetailView
from django.views.generic import UpdateView, DeleteView
from ..destination.models import Place
class LoginUserView(auth_views.LoginView):
template_name = "accounts/login_user.html"
class RegisterUserView(auth_views.CreateView):
template_name = "accounts/register_user.html"
form_class = MyHolidayUserCreationForm
success_url = reverse_lazy("index")
def form_valid(self, form):
result = super().form_valid(form)
login(self.request, self.object)
return result
def logout_user(request):
logout(request)
return redirect('login user')
class ProfileDetailsView(DetailView):
model = Profile
template_name = 'accounts/details_profile.html'
class ProfileUpdateView(UpdateView):
queryset = Profile.objects.all()
template_name = 'accounts/edit_profile.html'
fields = ("first_name", "last_name", "age", "profile_photo",)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save()
return super().form_valid(form)
def get_success_url(self):
return reverse("details profile", kwargs={"pk": self.object.pk})
class ProfileDeleteView(DeleteView):
model = Profile
template_name = 'accounts/delete_profile.html'
success_url = reverse_lazy("index")
def create_profile(request):
if request.method == 'POST':
form = ProfileCreationForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
return redirect('details profile', pk=profile.pk)
else:
form = ProfileCreationForm()
return render(request, 'accounts/create_profile.html', {'form': form})
3. Create create_profile.html
Create Profile
Create Your Profile
4. Update base.html so the button be CREATE PROFILE in navigation bar when the user is authenticated but still does not have profile. And when he creates profile the button change to PROFILE which go to details profile. And in details profile has buttons EDIT and DELETE profile.
{% load static %}
My Holiday