Model.py
-----------------------------
from django.db import models
# Create your models here.
class Contact(models.Model):
name = models.CharField(max_length = 100)
email = models.CharField(max_length = 100)
phone = models.IntegerField()
message = models.CharField(max_length = 1000)
def __str__(self):
return self.name
views.py
-----------------------------------
from django.shortcuts import render,redirect
from django.http import HttpResponse
from . forms import ContactForm
from . models import Contact
# Create your views here.
def index(request):
return render(request,'index.html')
def contact(request):
if request.method == "POST":
form = ContactForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('index')
else:
return render(request,'index.html')
forms.py
----------------------------
from django import forms
from . models import Contact
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ['name','email','phone','message']
urls.py
--------------------------
from django.urls import path
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('contact/', views.contact,name='contact'),
]
--------------------------------------
index.html
<div class="col-md-8" data-aos="fade-up">
<form action="{% url 'contact'%}" method="POST" class="contact-bg" id="contact" >
{% csrf_token %}
<input type="text" name="name" class="form-control" placeholder="Your Name" />
<input type="email" name="email" class="form-control" placeholder="Your E-mail" />
<input type="text" name="phone" class="form-control" placeholder="Phone Number" />
<textarea name="message" class="form-control" placeholder="Your Message" style="height:120px"></textarea>
<button id="submit" type="submit" name="submit" class="btn btn-glance">Send</button>
<div id="success">
<p class="green textcenter"> Your message was sent successfully! I will be in touch as soon as I can. </p>
</div>
<div id="error">
<p> Something went wrong, try refreshing and submitting the form again. </p>
</div>
</form>
</div>
-----------------------------
from django.db import models
# Create your models here.
class Contact(models.Model):
name = models.CharField(max_length = 100)
email = models.CharField(max_length = 100)
phone = models.IntegerField()
message = models.CharField(max_length = 1000)
def __str__(self):
return self.name
views.py
-----------------------------------
from django.shortcuts import render,redirect
from django.http import HttpResponse
from . forms import ContactForm
from . models import Contact
# Create your views here.
def index(request):
return render(request,'index.html')
def contact(request):
if request.method == "POST":
form = ContactForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('index')
else:
return render(request,'index.html')
forms.py
----------------------------
from django import forms
from . models import Contact
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ['name','email','phone','message']
urls.py
--------------------------
from django.urls import path
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('contact/', views.contact,name='contact'),
]
--------------------------------------
index.html
<div class="col-md-8" data-aos="fade-up">
<form action="{% url 'contact'%}" method="POST" class="contact-bg" id="contact" >
{% csrf_token %}
<input type="text" name="name" class="form-control" placeholder="Your Name" />
<input type="email" name="email" class="form-control" placeholder="Your E-mail" />
<input type="text" name="phone" class="form-control" placeholder="Phone Number" />
<textarea name="message" class="form-control" placeholder="Your Message" style="height:120px"></textarea>
<button id="submit" type="submit" name="submit" class="btn btn-glance">Send</button>
<div id="success">
<p class="green textcenter"> Your message was sent successfully! I will be in touch as soon as I can. </p>
</div>
<div id="error">
<p> Something went wrong, try refreshing and submitting the form again. </p>
</div>
</form>
</div>
Comments
Post a Comment