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"),
]