Search code examples
djangodjango-modelsdjango-formsdjango-templatesdjango-views

How to use crud operation in django?


I facing the issue in the Django crud operation. I tried to create a crud operation using Django. All operations are working fine but update operation only is not working. because I tried to click the edit button and show the data in the edit form and also change the data in the form and click the update button in the URL is changing this type (http://localhost:8000/show/6/update/6) . I need this URL (http://localhost:8000/update/6). I tried many ways but I could not get an error where is occurred.any one can run my code and give comments. How to solve this issue.

Project

urls.py

from django.contrib import admin
from django.conf.urls import url,include

urlpatterns = [
    url('admin/', admin.site.urls),
    url(r'^',include('olcapp.urls'))
]

Application

urls.py

from django.conf.urls import url
from olcapp import views

urlpatterns = [
    url(r'^$',views.get),
    url(r'^post/$',views.post),
    url(r'^show/([0-9]+)/$',views.getId),
    url(r'^update/([0-9]+)/$',views.update,name='update'),
    url(r'^delete/([0-9]+)/$',views.delete),
]

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import CrudOperation
from .forms import CrudForm

# Create your views here.
def get(request):
    form = CrudForm()
    data = CrudOperation.objects.all()
    return render(request,'index.html',{'form':form,'data':data})

def getId(request,id):
    form = CrudForm()
    data = CrudOperation.objects.get(id=id)
    return render(request,'edit.html',{'form':form,'data':data})

def post(request):
    form = CrudForm(request.POST)
    if form.is_valid():
        form.save()
    return HttpResponse('<h1>post Method</h1>')

def update(request,id):
    print(id)
    data = CrudOperation.objects.get(id=id)
    form = CrudForm(request.POST,instance=data)
    if(form.is_valid()):
        form.save()
    return HttpResponse('<h1>update method</h1>')

def delete(request,id):
    data = CrudOperation.objects.get(id=id)
    data.delete()
    return HttpResponse('<h1>Delete method</h1>')

Templates

index.html

<h1> Registration Form </h1>
<form action="post/" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="submit">
</form>

<h1>List of all user</h1>  
<table style="width:100%">
        <tr>
          <th>Name</th>
          <th>Email</th>
        </tr>
        {% for i in data %}
        <tr>
          <td>{{ i.name }}</td>
          <td>{{ i.email}}</td>
          <td><a href="show/{{ i.id }}/">Edit</a></td>
          <td><a href="delete/{{ i.id }}/">Delete</a></td>
        </tr>
        {% endfor %}
      </table>

edit.html

<h1> Update Form </h1>
<form action="update/{{ data.id }}" method="POST">
    {% csrf_token %}
    <label>Name</label> 
    <input type="textbox" name="name" value="{{ data.name }}"> 
    <label>Email</label> 
    <input type="textbox" name="email" value="{{ data.email }}"> 
    <input type="submit" value="Update">
</form>

Solution

  • To create a Django application that performs CRUD operations, follow the following steps. 1. Create a Project $ django-admin startproject crudexample
    2. Create an App $ python3 manage.py startapp employee 3. Project Structure

    4.Run the command to migrate the migrations.

    $ python3 manage.py migrate

    5.Run Server To run server use the following command.

    $ python3 manage.py runserver

    Access to the Browser Access the application by entering localhost:8000/show, it will show all the available employee records. Initially, there is no record.

    Adding Record Click on the Add New Record button and fill the details.

    This section also allows, update and delete records from the actions column. After saving couple of records, now we have following records.

    Update Record Lets update the record of x by clicking on edit button. It will display record of x in edit mode.

    Lets, suppose I update x to x kumar then click on the update button. It updates the record immediately. Click on update button and it redirects to the following page. See name is updated. Same like, we can delete records too, by clicking the delete link.

    Delete Record Suppose, I want to delete x, it can be done easily by clicking the delete button.