Tutorial 3: sql command(Insert)

Come to previous model i change the field user to ename

model.py
======
from django.db import models


class employee(models.Model):
    ename = models.CharField(max_length=100)







terminal
=======

erp@erp-Inspiron-N5110:~/dml$ python manage.py makemigrations
Did you rename employee.user to employee.ename (a CharField)? [y/N] y
Migrations for 'dmlapp':
  0002_auto_20150918_0731.py:
    - Rename field user on employee to ename
erp@erp-Inspiron-N5110:~/dml$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, dmlapp, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying dmlapp.0002_auto_20150918_0731... OK








url.py
=====

from django.conf.urls import include, url
from django.contrib import admin
from dmlapp.views import *
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$','dmlapp.views.index', name='index'),
    url(r'^insert/$','dmlapp.views.insert', name='insert'),
    url(r'^delete/$','dmlapp.views.delete', name='delete'),
    url(r'^update/$','dmlapp.views.update', name='update'),


]





insert.html
========

{% block content %}


    <h2>Adding User</h1>
    <form action="{% url 'insert' %}" method="POST" >{% csrf_token %}

    <table>
    <tr><td>employee Name :</td><td>{{form.ename}}</td><td>{{ form.ename.errors }}</td></tr>
    <tr><td></td><td><input type="submit" value="Save Changes" /></td></tr>
    </table>
</form>
{% endblock %}







form.py
=====

from dmlapp.models import employee
from django import forms

import warnings


class EmployeeForm(forms.Form):
    ename = forms.CharField(max_length=200,label=("employee Name:"), required=True)





view.py
=====





############### standard library files##########
from django.shortcuts import *
from django.http import *


########### support files/library/etc.... of app #################
from dmlapp.forms import EmployeeForm
from dmlapp.models import employee


################## insert query in django.######################

def insert(request):
    ename=request.POST.get('ename')    
################## this will give you the  textbox #################
    if request.method == 'GET':
        form = EmployeeForm()
    else:
         form = EmployeeForm(request.POST)
################ insert query in django    ##############################
         if form.is_valid():
             a = employee(ename=ename)
             a.save()
             return redirect('/insert/')
    return render(request, 'insert.html', {'form': form})


######################## end of insert query ######################





def index(request):
    return render(request,'index.html')



def delete(request):
    return render(request,'delete.html')

def update(request):
    return render(request,'update.html')





in mysql
=====
mysql> select * from  dmlapp_employee;
Empty set (0.00 sec)




o/p
===

Adding User

employee Name :




enter the django in the test box

and press save changes






in mysql
======


mysql> select * from  dmlapp_employee;
+----+--------+
| id | ename  |
+----+--------+
|  6 | django |
+----+--------+
1 row in set (0.00 sec)









############ end of insert query using django  #################

No comments:

Post a Comment