{"id":8167,"date":"2020-08-29T11:48:10","date_gmt":"2020-08-29T11:48:10","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8167"},"modified":"2020-08-29T11:48:11","modified_gmt":"2020-08-29T11:48:11","slug":"django-crud-application","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/django\/django-crud-application","title":{"rendered":"Django CRUD Application"},"content":{"rendered":"\n<p>In this article, we will know what Django CRUD application consists of and then later create our very own CRUD application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a Django CRUD application?<\/strong><\/h2>\n\n\n\n<p>A CRUD application is a website that deals with the <strong>CRUD-Create, Retrieve, Update, and Delete<\/strong> operations.\u00a0 A typical example of a CRUD application is a <strong>Student data <\/strong>application. In such applications, you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Add\/Create <\/strong>new Student data<\/li><li><strong>Retrieve <\/strong>the present student\u2019s data<\/li><li><strong>Update\/Edit<\/strong> an already student\u2019s data<\/li><li><strong>Delete<\/strong> a student data<\/li><\/ul>\n\n\n\n<p>We will now learn about each of these operations <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"396\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-29.png\" alt=\"CRUD\" class=\"wp-image-8174\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-29.png 721w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-29-300x165.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><figcaption>CRUD<\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Create: <\/strong>Create or add new entries into the database<\/li><li><strong>Retrieve: <\/strong>Get the entries\/entry from the Database<\/li><li><strong>Update: <\/strong>Update a particular entry in the database<\/li><li><strong>Delete: <\/strong>Delete a particular entry from the database<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating our own CRUD application in Django<\/h2>\n\n\n\n<p>Let us now create a simple <strong>Student Data<\/strong> CRUD application. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Creating a Model Table<\/h3>\n\n\n\n<p>To store the data, we need to create a <a href=\"https:\/\/www.askpython.com\/django\/django-models\" class=\"rank-math-link\">Django model<\/a>. Hence add the following model into your file.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass StudentModel(models.Model):\n    id = models.IntegerField(primary_key = True)\n    student_name = models.CharField(max_length=80)\n    rollnumber = models.CharField(max_length=10)\n    student_class = models.IntegerField()\n    student_age = models.IntegerField()\n\n    def __str__(self):\n        return f&quot;{self.student_name} : {self.rollnumber}&quot;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Creating a Model Form<\/h3>\n\n\n\n<p>Also we will require a <a href=\"https:\/\/www.askpython.com\/django\/django-model-forms\" class=\"rank-math-link\">Model Form <\/a>to display the model fields to the users. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass StudentForm(forms.ModelForm):\n    class Meta:\n        model = StudentModel\n        fields = (&#039;id&#039;,&#039;student_name&#039;,&#039;rollnumber&#039;,&#039;student_class&#039;,&#039;student_age&#039;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Coding the Create View<\/h3>\n\n\n\n<p>In Views.py create the View <strong>&#8220;CreateView&#8221;<\/strong> and add the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom .models import StudentModel\nfrom .forms import StudentForm\nfrom django.shortcuts import render,redirect\n\ndef CreateView(request):\n    if request.method == &#039;POST&#039;:\n        form = StudentForm(request.POST)\n        if form.is_valid():\n            form.save()\n            return redirect(&#039;\/data&#039;)\n    else:\n        form =StudentForm()\n        context = {\n            &#039;form&#039;:form\n        }\n        return render(request,&#039;create.html&#039;,context)\n<\/pre><\/div>\n\n\n<p>The template <strong>&#8220;create.html<\/strong>&#8221; will look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;form method = &quot;post&quot;&gt;\n    {% csrf_token %}\n    {{form.as_p}}\n    &lt;input type = &quot;submit&quot; value = &quot;submit&quot;&gt;\n&lt;\/form&gt;\n<\/pre><\/div>\n\n\n<p>The URL path for the View will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npath(&#039;data\/create&#039;, CreateView),\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. Coding the Retrieve View<\/h3>\n\n\n\n<p>Now in the retrieve operation, there are two ways possible<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Display the all data list <strong>(<a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/django\/django-listview\">Django List View<\/a>)<\/strong><\/li><li>Display Only a particular data <strong>(<a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/django\/django-detailview\">Django Detail View<\/a>)<\/strong><\/li><\/ul>\n\n\n\n<p>Therefore, add both the following Views into your views.py file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.shortcuts import render,redirect\nfrom .models import StudentModel\nfrom .forms import StudentForm\n\ndef Retrieve_ListView(request):\n    dataset = StudentModel.objects.all()\n    return render(request,&#039;listview.html&#039;,{&#039;dataset&#039;:dataset})\n\ndef Retrieve_DetailView(request,_id):\n    try:\n        data =StudentModel.objects.get(id =_id)\n    except StudentModel.DoesNotExist:\n        raise Http404(&#039;Data does not exist&#039;)\n\n    return render(request,&#039;detailview.html&#039;,{&#039;data&#039;:data})\n\n<\/pre><\/div>\n\n\n<p>The Corresponding <strong>ListView <\/strong>template will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n{% for data in dataset %}\n{{data}}\n&lt;hr&gt;\n{% endfor %}\n<\/pre><\/div>\n\n\n<p>The Corresponding <strong>DetailView <\/strong>template will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;h3&gt;Name:{{data.student_name}}&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;Roll Number:{{data.rollnumber}}&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;Class:{{data.student_class}}&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;Age:{{data.student_age}}&lt;\/h3&gt;&lt;br&gt;\n&lt;hr\/&gt;\n<\/pre><\/div>\n\n\n<p>The URL paths for the Views will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n    path(&#039;data\/&#039;, Retrieve_ListView),\n    path(&#039;data\/&lt;int:_id&gt;&#039;,Retrieve_DetailView),\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. Coding the Update View<\/h3>\n\n\n\n<p>Now add the following Update View into the views.py file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.shortcuts import render,redirect,get_object_or_404\nfrom .models import StudentModel\nfrom .forms import StudentForm\n\ndef UpdateView(request,_id):\n    try:\n        old_data = get_object_or_404(StudentModel,id =_id)\n    except Exception:\n        raise Http404(&#039;Does Not Exist&#039;)\n\n    if request.method ==&#039;POST&#039;:\n        form =StudentForm(request.POST, instance =old_data)\n\n        if form.is_valid():\n            form.save()\n            return redirect(f&#039;\/data\/{_id}&#039;)\n    \n    else:\n\n        form = StudentForm(instance = old_data)\n        context ={\n            &#039;form&#039;:form\n        }\n        return render(request,&#039;update.html&#039;,context)\n<\/pre><\/div>\n\n\n<p>The corresponding <strong>update.html <\/strong>template will look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;form method=&quot;post&quot;&gt;\n    {% csrf_token %}\n    {{form.as_p}}\n    &lt;input type=&quot;submit&quot; value = &quot;Update&quot;&gt;\n&lt;\/form&gt;\n<\/pre><\/div>\n\n\n<p>The URL path for the View will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npath(&#039;data\/&lt;int:_id&gt;\/update&#039;, UpdateView),\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">6. Coding the Delete View<\/h3>\n\n\n\n<p>Now add the <strong>Delete View<\/strong> into your views.py file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef DeleteView(request,_id):\n    try:\n        data = get_object_or_404(StudentModel,id =_id)\n    except Exception:\n        raise Http404(&#039;Does Not Exist&#039;)\n\n    if request.method == &#039;POST&#039;:\n        data.delete()\n        return redirect(&#039;\/data&#039;)\n    else:\n        return render(request, &#039;delete.html&#039;)\n<\/pre><\/div>\n\n\n<p>The corresponding <strong>delete.html <\/strong>will look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;form method=&quot;post&quot;&gt;\n    {% csrf_token %}\n    Click YES to confirm\n    &lt;input type = &quot;submit&quot; value=&quot;YES&quot;&gt;\n    &lt;a href=&#039;\/data&#039;&gt;Cancel&lt;\/a&gt;\n&lt;\/form&gt;\n<\/pre><\/div>\n\n\n<p>The URL path for the View will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npath(&#039;data\/&lt;int:_id&gt;\/delete&#039;, DeleteView),\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Implementing the Student\u2019s App<\/strong><\/h2>\n\n\n\n<p>That&#8217;s it with the coding part !! Now fire up the server and let&#8217;s go to &#8220;<strong>\/data\/create<\/strong>&#8221; endpoint <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"405\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Create.png\" alt=\"Create\" class=\"wp-image-8169\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Create.png 696w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Create-300x175.png 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><figcaption>Create<\/figcaption><\/figure>\n\n\n\n<p>Hit submit and you will reach the <strong>&#8220;\/data&#8221;<\/strong> page where the list of students is displayed.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"405\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Retrieve-list-view.png\" alt=\"Retrieve List View\" class=\"wp-image-8170\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Retrieve-list-view.png 696w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Retrieve-list-view-300x175.png 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><figcaption>Retrieve List View<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now try the &#8220;<strong>\/data\/1<\/strong>&#8221; endpoint<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"461\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Retrieve-detail-view.png\" alt=\"Retrieve Detail View\" class=\"wp-image-8171\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Retrieve-detail-view.png 696w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Retrieve-detail-view-300x199.png 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><figcaption>Retrieve Detail View<\/figcaption><\/figure><\/div>\n\n\n\n<p>Okay guys !! lets us update the first entry. Go to &#8220;<strong>\/data\/1\/update<\/strong>&#8220;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"461\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Update.png\" alt=\"Update\" class=\"wp-image-8172\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Update.png 726w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Update-300x190.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><figcaption>Update<\/figcaption><\/figure><\/div>\n\n\n\n<p>Make some changes and hit Update, you will be redirected to the Detail View Page of the particular student.<\/p>\n\n\n\n<p>And lastly let&#8217;s try to delete the first student. Go to &#8220;<strong>\/data\/1\/delete<\/strong>&#8220;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"461\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Delete.png\" alt=\"Delete\" class=\"wp-image-8173\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Delete.png 726w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Delete-300x190.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><figcaption>Delete<\/figcaption><\/figure><\/div>\n\n\n\n<p>Hit <strong>YES<\/strong> and check, the particular student data will be removed from the DB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>That\u2019s it, Coders!! This was all about <strong>CRUD <\/strong>applications. Do check out the <strong><a href=\"https:\/\/www.askpython.com\/django\/django-rest-api\" class=\"rank-math-link\">Django REST API<\/a><\/strong> tutorial, which is the REST API version of a CRUD application. <\/p>\n\n\n\n<p>See you in the next article!! Till then keep coding !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will know what Django CRUD application consists of and then later create our very own CRUD application. What is a Django CRUD application? A CRUD application is a website that deals with the CRUD-Create, Retrieve, Update, and Delete operations.\u00a0 A typical example of a CRUD application is a Student data application. [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":8189,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-8167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8167"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8167\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8189"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}