Advertisement
horozov86

add create profile

Apr 12th, 2024
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 KB | None | 0 0
  1. 1. Update forms.py as I add ProfileCreationForm:
  2.  
  3. from django import forms
  4. from django.contrib.auth.forms import UserCreationForm, UserChangeForm
  5. from django.contrib.auth import get_user_model
  6. from django.contrib.auth.password_validation import validate_password
  7.  
  8. UserModel = get_user_model()
  9.  
  10.  
  11. class MyHolidayUserCreationForm(UserCreationForm):
  12.     user = None
  13.  
  14.     def clean_password2(self):
  15.         password2 = super().clean_password2()
  16.         validate_password(password2)
  17.         return password2
  18.  
  19.     class Meta(UserCreationForm.Meta):
  20.         model = UserModel
  21.         fields = ('email',)
  22.  
  23.  
  24. class MyHolidayUserChangeForm(UserChangeForm):
  25.     class Meta(UserChangeForm.Meta):
  26.         model = UserModel
  27.  
  28.  
  29. class ProfileCreationForm(forms.ModelForm):
  30.     class Meta:
  31.         model = Profile
  32.         fields = ['first_name', 'last_name', 'age', 'profile_photo']
  33.  
  34.  
  35. 2. Views will be:
  36.  
  37. from django.contrib.auth import views as auth_views, login, logout
  38. from django.contrib.auth.mixins import AccessMixin
  39. from django.shortcuts import redirect, get_object_or_404, render
  40. from django.urls import reverse_lazy, reverse
  41. from my_holiday.accounts.forms import MyHolidayUserCreationForm, ProfileCreationForm
  42. from my_holiday.accounts.models import Profile
  43. from django.views.generic import DetailView
  44. from django.views.generic import UpdateView, DeleteView
  45. from ..destination.models import Place
  46.  
  47.  
  48. class LoginUserView(auth_views.LoginView):
  49.     template_name = "accounts/login_user.html"
  50.  
  51.  
  52. class RegisterUserView(auth_views.CreateView):
  53.     template_name = "accounts/register_user.html"
  54.     form_class = MyHolidayUserCreationForm
  55.     success_url = reverse_lazy("index")
  56.  
  57.     def form_valid(self, form):
  58.         result = super().form_valid(form)
  59.         login(self.request, self.object)
  60.         return result
  61.  
  62.  
  63. def logout_user(request):
  64.     logout(request)
  65.     return redirect('login user')
  66.  
  67.  
  68. class ProfileDetailsView(DetailView):
  69.     model = Profile
  70.     template_name = 'accounts/details_profile.html'
  71.  
  72.  
  73. class ProfileUpdateView(UpdateView):
  74.     queryset = Profile.objects.all()
  75.     template_name = 'accounts/edit_profile.html'
  76.     fields = ("first_name", "last_name", "age", "profile_photo",)
  77.  
  78.     def form_valid(self, form):
  79.         self.object = form.save(commit=False)
  80.         self.object.save()
  81.         return super().form_valid(form)
  82.  
  83.     def get_success_url(self):
  84.         return reverse("details profile", kwargs={"pk": self.object.pk})
  85.  
  86.  
  87. class ProfileDeleteView(DeleteView):
  88.     model = Profile
  89.     template_name = 'accounts/delete_profile.html'
  90.     success_url = reverse_lazy("index")
  91.  
  92.  
  93. def create_profile(request):
  94.     if request.method == 'POST':
  95.         form = ProfileCreationForm(request.POST, request.FILES)
  96.         if form.is_valid():
  97.             profile = form.save(commit=False)
  98.             profile.user = request.user
  99.             profile.save()
  100.             return redirect('details profile', pk=profile.pk)
  101.     else:
  102.         form = ProfileCreationForm()
  103.     return render(request, 'accounts/create_profile.html', {'form': form})
  104.  
  105. 3. Create create_profile.html
  106.  
  107.  
  108. <!DOCTYPE html>
  109. <html lang="en">
  110. <head>
  111.     <meta charset="UTF-8">
  112.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  113.     <title>Create Profile</title>
  114. </head>
  115. <body>
  116.     <h2>Create Your Profile</h2>
  117.     <form method="post" enctype="multipart/form-data">
  118.         {% csrf_token %}
  119.         {{ form.as_p }}
  120.         <button type="submit">Create Profile</button>
  121.     </form>
  122. </body>
  123. </html>
  124.  
  125. 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.
  126.  
  127. {% load static %}
  128. <!DOCTYPE html>
  129. <html lang="en">
  130. <head>
  131.     <meta charset="UTF-8">
  132.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  133.     <link rel="stylesheet" href="{% static 'style/style.css' %}">
  134.     <link rel="stylesheet" href="{% static 'style/login_user.css' %}">
  135.     <link rel="stylesheet" href="{% static 'style/register_user.css' %}">
  136.  
  137.     <link rel="icon" href="{% static 'images/favicon.png' %}" type="image/x-icon">
  138.     <title>My Holiday</title>
  139. </head>
  140. <body>
  141. <div id="box">
  142.     <header>
  143.         <!-- Navigation Bar -->
  144.         <h1>
  145.             <a class="index-navigation" href="{% url 'index' %}"><span>My Holiday</span></a>
  146.         </h1>
  147.         <nav>
  148.             <a href="{% url 'travelogue_view' %}">Travelogue</a>
  149.             <a href="{% url 'create_place' %}">Add Place</a>
  150.             {% if request.user.is_authenticated %}
  151.                 {% if request.user.profile %}
  152.                     <a href="{% url 'details profile' pk=request.user.profile.pk %}">Profile</a>
  153.                 {% else %}
  154.                     <a href="{% url 'create_profile' %}">Create Profile</a>
  155.                 {% endif %}
  156.                 <a href="{% url 'logout user' %}">Logout</a>
  157.             {% else %}
  158.                 <a href="{% url 'login user' %}">Login</a>
  159.                 <a href="{% url 'register user' %}">Register</a>
  160.             {% endif %}
  161.         </nav>
  162.     </header>
  163.  
  164.     {% block main_content %}
  165.     {% endblock %}
  166.  
  167.     <footer>&copy;Django project My Holiday. All rights reserved.</footer>
  168. </div>
  169. </body>
  170. </html>
  171.  
  172.  
  173.  
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement