Example : Python-MySql Connectivity Code using Pycharm.
views.py         (Of already created inside Apps file)

 def DataSaved(request):
    
---------------- Connectivity Code ----------------

    conn = pymysql.connect(host='localhost', user='root', passwd='', db='databasename')
    mycursor = conn.cursor()
    
--------- Code to retrieve data from html page to python variables to save it --------

    TxtSlno = request.POST.get('TxtSlno1')
    print(TxtSlno)
    TxtUname = request.POST.get('TxtUname1')
    print("User Name", TxtUname)
    TxtPsd = request.POST.get('TxtPsd1')    
    DateDob = request.POST.get('DateDob1')    
    RdGender = request.POST.get('RdbGender1')    
    NumMob = request.POST.get('NumMob1')    
    TxtEmail = request.POST.get('TxtEmail1')    
    ChkMatric = request.POST.get('ChkMatric1')    
    ChkInter3 = request.POST.get('ChkInter1')    
    ChkGrad3 = request.POST.get('ChkGrad1')    
    TxrAdd3 = request.POST.get('TxrAdd1')    
    CmbState3 = request.POST.get('CmbState1')    
    TxrRemarks = request.POST.get('TxrRemarks')

---------------------- Save Code -----------------------
    try:
        conn.cursor()
        store = (TxtSlno, TxtUname, TxtPsd, DateDob, RdGender, NumMob, TxtEmail, ChkMatric, 
           ChkInter3, ChkGrad3, TxrAdd3, CmbState3, TxrRemarks)
        mycursor.execute("" "insert into userreg(Slno,Uname,Psd,Dob,Gen,Mob, Email, Matric,
         Inter, Grad, Address, State,Remarks) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" 
          "", store)
        conn.commit()
        msg = "Data Saved"
        print(msg)
    except NameError:
        print("Wrong Data Entry..!!")
    finally:
        mycursor.close()
        conn.close()

    return render(request, 'UserReg.html', {'msg1': msg})

--------------------------------------------------------------------------
UserReg.html     (Of project template folder)

   <!DOCTYPE html>
      <html lang="en">
         <head>
            <meta charset="UTF-8">
            <title>Codershelpline</title>
         </head>
         <body>
            {{ msg1 }}

<form method="post">{% csrf_token %}
    <center>
        <fieldset style="width: 50%">
            <table>
                <h1> User Registration</h1>
                <hr> 
              
                <tr>
                    <td>Serial No.</td>
                    <td>:</td>
                    <td>
                        <input type="text" name="TxtSlno1" autofocus="" placeholder="Enter 
                             Serial No.">
                    </td>
                </tr>

                <tr>
                    <td>User Name</td>
                    <td>:</td>
                    <td>
                        <input type="text" name="TxtUname1" autofocus="" placeholder="Enter 
                              User ID" >    
                    </td>
                </tr>

                <tr>
                    <td>Password</td>
                    <td>:</td>
                    <td>
                        <input type="password" name="TxtPsd1" placeholder="Enter Password">
                    </td>
                </tr>

                <tr>
                    <td>Date of Birth</td>
                    <td>:</td>
                    <td>
                        <input type="date" name="DateDob1" >
                    </td>
                </tr>

                <tr>
                    <td>Gender</td>
                    <td>:</td>
                    <td>
                        <input type="radio" name="RdbGender1" id="UrRdbGender2" value="Male">
                            Male
                        <input type="radio" name="RdbGender1" id="UrRdbGender3" value=
                            "Female">Female
                    </td>
                </tr>

                <tr>
                    <td>Mobile No.</td>
                    <td>:</td>
                    <td>
                        <input type="number" name="NumMob1" placeholder="Enter 10 digit No.">
                    </td>
                </tr>

                <tr>
                    <td>E-mail</td>
                    <td>:</td>
                    <td>
                        <input type="Email" name="TxtEmail1" placeholder="[email protected]">
                    </td>
                </tr>

                <tr>
                    <td>Qualification</td>
                    <td>:</td>
                    <td>
                        <input type="checkbox" name="ChkMatric1"> Matric
                        <input type="checkbox" name="ChkInter1"> Intermediate
                        <input type="checkbox" name="ChkGrad1">Graduation
                    </td>
                </tr>

                <tr>
                    <td>Address</td>
                    <td>:</td>
                    <td>
                        <textarea name="TxrAdd1"></textarea>
                    </td>
                </tr>

                <tr>
                    <td>State</td>
                    <td>:</td>
                    <td>
                        <select name="CmbState1" id="CmbState2">
                            <option value="Select">Select</option>
                            <option value="Bihar" >Bihar</option>
                            <option value="UP">Uttar Pradesh</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>Remarks</td>
                    <td>:</td>
                    <td>
                        <textarea name="TxrRemarks"></textarea>
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td></td>
                    <td>
                        <input type="submit" class="btn btn-primary" value="Save" 
                             formaction="{% url 'savedata1' %}">
                        <input type="reset" class="btn btn-dark" value="Reset">                        
                    </td>
                </tr>

            </table>
          </fieldset>
        </center>
     </form>  
   </body>
 </html>

--------------------------------------------------------------------
urls.py          (Of/create inside Apps file)

  from django.urls import path
  from . import views

  urlpatterns = [
     path('', views.connect),   # Opens connect function of views.py file automatically as 
       default.
     path('DataSaved', views.DataSaved, name='savedata1'),
  ]

 224 total views,  2 views today

Categories: Python

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.