duplicate entry in mysql database using FormModel and html form

i am using django ModelForm with createView but problem is that , i want to submit html contact form by user submitting.

but happened like i submit the html form using createView then to see duplicate entry in mysql database.

Html Form:-

<form action="/submit/"
          class="u-clearfix u-form-spacing-20 u-form-vertical u-inner-form" style="padding: 10px" source="email"
          name="form" method="POST">
          {% csrf_token %}
          <div class="u-form-group u-form-name u-label-none">
            <label for="name-3b9a15" class="u-label">Name</label>
              
            <input type="text" placeholder="Enter your Name" id="name-3b9a15" name="name"
              class="u-border-2 u-border-black u-border-no-left u-border-no-right u-border-no-top u-input u-input-rectangle u-input-1"
              required>
          </div>
          <div class="u-form-email u-form-group u-label-none">
            <label for="email-3b9a" class="u-label">Email</label>
            <input type="email" placeholder="Enter a valid email address" id="email-3b9a" name="email"
              class="u-border-2 u-border-black u-border-no-left u-border-no-right u-border-no-top u-input u-input-rectangle u-input-2"
              required>
          </div>
          <div class="u-form-group u-form-message u-label-none">
            <label for="message-3b9a" class="u-label">Message</label>
            <textarea placeholder="Enter your message" rows="4" cols="50" id="message-3b9a" name="message"
              class="u-border-2 u-border-black u-border-no-left u-border-no-right u-border-no-top u-input u-input-rectangle u-input-3"
              required></textarea>
          </div>
          <button type="submit" style="margin: 0px auto;" class="u-border-2 u-border-black u-btn u-btn-rectangle u-btn-submit u-button-style u-none u-btn-1">Submit</button>
        </form>

View.py

class ContactView(TemplateView):
    template_name = "Contact.html"

    def get_context_data(self, **kwargs):
        contact = super().get_context_data()
        return contact



class ContactFormView(CreateView):
    model = ContactModel
    form_class = ContactFormModel
    template_name = "success.html"
    success_url = "/submit/"

    def form_valid(self, form):
        print(form)
        return super().form_valid(form)

forms.py:-

from django.forms import ModelForm
from .models import ContactModel
class ContactFormModel(ModelForm):

    class Meta:
        model = ContactModel
        fields = ["name", "email", "message"]

model.py

class ContactModel(models.Model):
    id = models.AutoField
    name = models.CharField(max_length=25)
    email = models.EmailField()
    message = models.TextField(max_length=4000)
    created_on = models.DateTimeField(auto_now_add=True)

urls.py:

urlpatterns = [

    path('', HomeView.as_view(), name="index"),
    path('about/', AboutView.as_view(), name="about"),
    path('contact/', ContactView.as_view(), name="contact"),
    path('cart/', CartView.as_view(), name="cart"),
    path('submit/', ContactFormView.as_view(), name="submit1"),
    path('checkout/<slug:slug>', CheckoutView.as_view(), name="checkout"),
    path('VitaminGummies/', VitaminGummiesView.as_view(), name="DB"),
    path('VitaminGummies/<slug:slug>', VitaminGummiesView.as_view(), name="SlugView"),

]

To get some help with your issue, you must give some information about how you implemented this.

At least, can you share your model, form and view.

Its, updated you can see that and give answer thank you

You claimed you used a CreateView but this is not the case here: you use a simple View, where the model class attribute is useless.

Also, you have a ModelForm but it is not used anywhere.

Additionnally, your template is incomplete as we do not have the form opening tag with the action.

The code you provided is not consistent with your issue. If you really use a CreateView, I guess you may have a problem with your overrides where the model is save the first time by the CreateView.form_valid through the ModelForm, and a second time in your override of the view when you create the model by yourself in the view.

Can you also share your urls.py file content ?

updated and sorry its try and error View method .

Can you show the full Contact.html template (in particular with the opening form tag.

Also, can you ensure that all the code for model, modelform and create view are shown in your post (i.e. no specific implementation of other methods)?

If so, there should not be duplicate creations. Do you have some logs of received requests server side (either in the console used to launch the runserver command if you are in development mode, or in your server - e.g. apache - logs otherwise): this is to ensure form submission request is not emitted twice, e.g. by double clicking the submit button.

  1. This is the full form tags and other content is Base.html is inherited no other implementation in contact.html.
  2. and model, modelform createview is a single time declared.
  3. and i am clicking only one time but any scenario i click twice but every time duplicate entry is stored.

Again, can you show the complete Contact.html template? In your post, at least the <form> opening tag is missing.

To ensure correct rendering of code included in posts, you must enclose it between 3 backticks:
```
<your code here>
```

Also, is there some javascript handling of form submission in the browser?

<form action="/submit/"
          class="u-clearfix u-form-spacing-20 u-form-vertical u-inner-form" style="padding: 10px" source="email"
          name="form" method="POST">
          {% csrf_token %}
          <div class="u-form-group u-form-name u-label-none">
            <label for="name-3b9a15" class="u-label">Name</label>
              
            <input type="text" placeholder="Enter your Name" id="name-3b9a15" name="name"
              class="u-border-2 u-border-black u-border-no-left u-border-no-right u-border-no-top u-input u-input-rectangle u-input-1"
              required>
          </div>
          <div class="u-form-email u-form-group u-label-none">
            <label for="email-3b9a" class="u-label">Email</label>
            <input type="email" placeholder="Enter a valid email address" id="email-3b9a" name="email"
              class="u-border-2 u-border-black u-border-no-left u-border-no-right u-border-no-top u-input u-input-rectangle u-input-2"
              required>
          </div>
          <div class="u-form-group u-form-message u-label-none">
            <label for="message-3b9a" class="u-label">Message</label>
            <textarea placeholder="Enter your message" rows="4" cols="50" id="message-3b9a" name="message"
              class="u-border-2 u-border-black u-border-no-left u-border-no-right u-border-no-top u-input u-input-rectangle u-input-3"
              required></textarea>
          </div>
          <button type="submit" style="margin: 0px auto;" class="u-border-2 u-border-black u-btn u-btn-rectangle u-btn-submit u-button-style u-none u-btn-1">Submit</button>
        </form>

yes, i check use any javascript for insert data or submit form

@antoinehumbert Thank for helping me out of the solution i am using javascript to submit the form previously and i completely forget that , and thank for remember that. issues is solved by removing javascript.