Tutorial 4: sql command(select)

Views.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 ######################

################# select query in django####################

def select(request):
    emp=employee.objects.all()
    return render(request,'select.html',{'emp':emp})



################# end of select 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')






urls.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'),
    url(r'^select/$','dmlapp.views.select', name='select'),

]






select.html
======



{% block content %}


    <h2>employee list</h1>
{% for emp in emp %}

<ul><li>
{{emp.ename}}
</li></ul>
{% endfor %}

{% endblock %}





o/p

===



employee list

  • django
  • python


No comments:

Post a Comment