Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Create Profile</title>
- </head>
- <body>
- <h2>Create Your Profile</h2>
- <form method="post" enctype="multipart/form-data">
- {% csrf_token %}
- {{ form.as_p }}
- <button type="submit">Create Profile</button>
- </form>
- </body>
- </html>
- 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 %}
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="{% static 'style/style.css' %}">
- <link rel="stylesheet" href="{% static 'style/login_user.css' %}">
- <link rel="stylesheet" href="{% static 'style/register_user.css' %}">
- <link rel="icon" href="{% static 'images/favicon.png' %}" type="image/x-icon">
- <title>My Holiday</title>
- </head>
- <body>
- <div id="box">
- <header>
- <!-- Navigation Bar -->
- <h1>
- <a class="index-navigation" href="{% url 'index' %}"><span>My Holiday</span></a>
- </h1>
- <nav>
- <a href="{% url 'travelogue_view' %}">Travelogue</a>
- <a href="{% url 'create_place' %}">Add Place</a>
- {% if request.user.is_authenticated %}
- {% if request.user.profile %}
- <a href="{% url 'details profile' pk=request.user.profile.pk %}">Profile</a>
- {% else %}
- <a href="{% url 'create_profile' %}">Create Profile</a>
- {% endif %}
- <a href="{% url 'logout user' %}">Logout</a>
- {% else %}
- <a href="{% url 'login user' %}">Login</a>
- <a href="{% url 'register user' %}">Register</a>
- {% endif %}
- </nav>
- </header>
- {% block main_content %}
- {% endblock %}
- <footer>©Django project My Holiday. All rights reserved.</footer>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement