Skip to main content

Form basic django

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>


Comments

Popular posts from this blog

Connect POSTgreSQL with Django

  First install all necessary step  $ sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib Second $ sudo su - postgres $ psql Create User name with password  $ CREATE USER user_name WITH PASSWORD ' password ';   Create New Database $ CREATE DATABASE database_name WITH OWNER user_name ; Give permission  $ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user_name ; Update settings.py  DATABASES = { ' default ' : { ' ENGINE ' : ' django.db.backends.postgresql_psycopg2 ' , ' NAME ' : 'database_name ' , ' USER ' : ' user_name ' , ' PASSWORD ' : ' password' , ' HOST ' : ' localhost ' , ' PORT ' : ' 5432 ' , } }  Then, python3 manage.py migrate and finally create super user python3 man age.py createsuperuser