Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- 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.
- Here's how you can modify your template to remove the manual file input:
- html
- Copy code
- {% extends 'base.html' %}
- {% block main_content %}
- <section id="create-page" class="auth">
- <h2>Add Place</h2>
- <form id="create-place-form" action="{% url 'create_place' %}" method="post" enctype="multipart/form-data" novalidate>
- <div class="container">
- {{ form }}
- <input class="btn" type="submit" value="Add Place" />
- {% csrf_token %}
- </div>
- </form>
- </section>
- <script>
- document.addEventListener('DOMContentLoaded', function() {
- const form = document.querySelector('#create-place-form');
- form.addEventListener('submit', function(event) {
- const placeNameInput = document.querySelector('#id_name');
- const locationInput = document.querySelector('#id_location');
- const descriptionInput = document.querySelector('#id_description');
- let errors = [];
- if (!placeNameInput.value.trim()) {
- errors.push('Please enter a place name.');
- }
- if (!locationInput.value.trim()) {
- errors.push('Please enter a location.');
- }
- if (!descriptionInput.value.trim()) {
- errors.push('Please enter a description.');
- }
- if (errors.length > 0) {
- event.preventDefault(); // Prevent form submission
- const errorContainer = document.createElement('div');
- errorContainer.classList.add('error-container');
- errors.forEach(function(errorMsg) {
- const errorParagraph = document.createElement('p');
- errorParagraph.textContent = errorMsg;
- errorContainer.appendChild(errorParagraph);
- });
- const existingErrorContainer = form.querySelector('.error-container');
- if (existingErrorContainer) {
- existingErrorContainer.remove(); // Remove existing error messages
- }
- form.insertBefore(errorContainer, form.firstChild); // Insert error messages at the top of the form
- }
- });
- });
- </script>
- {% endblock %}
- 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.
- User
- 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?
- ChatGPT
- 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.
- 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement