Advertisement
horozov86

Solve the problem with doubled button for video

Apr 9th, 2024
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. The additional button that appears when you click on "Add Place" might be due to the <input type="file" name="video_url" accept="videos/*"> element that you added in your form. You are already using Django's built-in form handling, so you don't need to manually add another file input element.
  2.  
  3. To resolve this issue, you should remove the manual file input from your template and let Django handle the file upload through the video field in your PlaceCreateForm.
  4.  
  5. Here's how you can modify your template to remove the manual file input:
  6.  
  7. html
  8. Copy code
  9. {% extends 'base.html' %}
  10.  
  11. {% block main_content %}
  12.    <section id="create-page" class="auth">
  13.        <h2>Add Place</h2>
  14.        <form id="create-place-form" action="{% url 'create_place' %}" method="post" enctype="multipart/form-data" novalidate>
  15.            <div class="container">
  16.                {{ form }}
  17.                <input class="btn" type="submit" value="Add Place" />
  18.                {% csrf_token %}
  19.            </div>
  20.        </form>
  21.    </section>
  22.  
  23.    <script>
  24.        document.addEventListener('DOMContentLoaded', function() {
  25.            const form = document.querySelector('#create-place-form');
  26.  
  27.             form.addEventListener('submit', function(event) {
  28.                 const placeNameInput = document.querySelector('#id_name');
  29.                 const locationInput = document.querySelector('#id_location');
  30.                 const descriptionInput = document.querySelector('#id_description');
  31.  
  32.                 let errors = [];
  33.  
  34.                 if (!placeNameInput.value.trim()) {
  35.                     errors.push('Please enter a place name.');
  36.                 }
  37.  
  38.                 if (!locationInput.value.trim()) {
  39.                     errors.push('Please enter a location.');
  40.                 }
  41.  
  42.                 if (!descriptionInput.value.trim()) {
  43.                     errors.push('Please enter a description.');
  44.                 }
  45.  
  46.                 if (errors.length > 0) {
  47.                     event.preventDefault(); // Prevent form submission
  48.  
  49.                     const errorContainer = document.createElement('div');
  50.                     errorContainer.classList.add('error-container');
  51.  
  52.                     errors.forEach(function(errorMsg) {
  53.                         const errorParagraph = document.createElement('p');
  54.                         errorParagraph.textContent = errorMsg;
  55.                         errorContainer.appendChild(errorParagraph);
  56.                     });
  57.  
  58.                     const existingErrorContainer = form.querySelector('.error-container');
  59.                     if (existingErrorContainer) {
  60.                         existingErrorContainer.remove(); // Remove existing error messages
  61.                     }
  62.  
  63.                     form.insertBefore(errorContainer, form.firstChild); // Insert error messages at the top of the form
  64.                 }
  65.             });
  66.         });
  67.     </script>
  68. {% endblock %}
  69. By removing the manual file input, you ensure that only the file input associated with the video field in your form is processed, eliminating the appearance of an extra button.
  70.  
  71.  
  72. User
  73. So, you have in mind that this row <input type="file" name="video_url" accept="videos/*"> is not necessary in place_from.html, because Django takes this from the form, right? Am I understood correctly?
  74. ChatGPT
  75. Yes, that's correct. When you define a FileField in your Django form, Django automatically generates the necessary HTML input elements to handle file uploads. Therefore, manually adding <input type="file"> elements in your template for fields like video is unnecessary and can lead to unexpected behavior, such as the appearance of an extra button.
  76.  
  77. By relying on Django's form handling, you ensure that file uploads are processed correctly and consistently with the rest of your form fields. So, you don't need to manually add <input type="file"> elements for file fields defined in your Django forms.
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement