Advertisement
horozov86

details_profile.html with client-side validation

Apr 3rd, 2024
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. {% extends 'base.html' %}
  2. {% load static %}
  3. {% block main_content %}
  4. <link rel="stylesheet" href="{% static 'style/details_profile.css' %}">
  5. <h1>Profile Details</h1>
  6. <div class="info-section">
  7.     {% if profile.profile_photo %}
  8.     <img class="car-img" src="{{ profile.profile_photo.url }}" alt="profile-image" />
  9.     {% else %}
  10.     <img class="car-img" src="{% static 'images/default_profile_image.png' %}" alt="profile-image" />
  11.     {% endif %}
  12.     {% if profile.first_name or profile.last_name %}
  13.     {% if profile.first_name and profile.last_name %}
  14.     <h1>{{ profile.first_name }} {{ profile.last_name }}</h1>
  15.     {% elif profile.first_name %}
  16.     <h1>{{ profile.first_name }}</h1>
  17.     {% elif profile.last_name %}
  18.     <h1>{{ profile.last_name }}</h1>
  19.     {% endif %}
  20.     {% endif %}
  21.  
  22.     <p class="description">Email: {{ user.email }}</p>
  23.  
  24.     <p class="description">Age: {{ profile.age }}</p>
  25.  
  26.     <p class="description">Total photos: {{ place_count }}</p>
  27.  
  28.     <div class="buttons">
  29.         <a href="{% url 'edit profile' pk=object.pk %}" class="edit-button">Edit</a>
  30.         <a href="{% url 'delete profile' pk=object.pk  %}" class="delete-button">Delete</a>
  31.     </div>
  32. </div>
  33.  
  34. <form method="post" enctype="multipart/form-data" id="profile-form">
  35.     {% csrf_token %}
  36.     {{ form.as_p }}
  37. </form>
  38.  
  39. <script>
  40.     document.addEventListener('DOMContentLoaded', function() {
  41.         const form = document.querySelector('#profile-form');
  42.         const photoInput = document.querySelector('#id_profile_photo');
  43.  
  44.         form.addEventListener('submit', function(event) {
  45.             if (photoInput.files.length > 0) {
  46.                 const maxFileSize = 3 * 1024 * 1024; // 3 MB
  47.                 if (photoInput.files[0].size > maxFileSize) {
  48.                     alert('The file size exceeds the maximum limit of 3 MB.');
  49.                     event.preventDefault();
  50.                     return;
  51.                 }
  52.             }
  53.         });
  54.     });
  55. </script>
  56. {% endblock %}
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement