{"id":3949,"date":"2020-11-24T17:29:37","date_gmt":"2020-11-24T11:59:37","guid":{"rendered":"https:\/\/cns72.com\/vytcdc.com.sg\/?p=3949"},"modified":"2020-12-10T20:49:11","modified_gmt":"2020-12-10T15:19:11","slug":"python-django-crud-web-application","status":"publish","type":"post","link":"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/","title":{"rendered":"Python Django Crud Web Application"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>Django is a free, open-source framework for web applications written in Python. Django offers a large assortment of modules you can use in your projects. Firstly, there are frameworks to save developers a lot of time and headaches wasted, and Django is no different.<\/p>\n<div id=\"attachment_3951\" style=\"width: 717px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-3951\" loading=\"lazy\" class=\" wp-image-3951\" src=\"https:\/\/cns72.com\/vytcdc.com.sg\/wp-content\/uploads\/2020\/11\/python1.jpg\" alt=\"\" width=\"707\" height=\"303\" \/><p id=\"caption-attachment-3951\" class=\"wp-caption-text\">python django crud<\/p><\/div>\n<p>In this post, we briefly cover the steps needed to create a CRUD app in Django; the steps we will need are:<br \/>\n\u2022 Install Django and start a new project<br \/>\n\u2022 Make an App<br \/>\n\u2022 Create the Model<br \/>\n\u2022 Make the Admin Interface (optional)<br \/>\n\u2022 Create the View<br \/>\n\u2022 Define the URLs (i.e., URL to View mapping)<br \/>\n\u2022 Create the Templates<\/p>\n<p><strong>Install Django and Start New Project<\/strong><\/p>\n<pre><strong>django-admin startproject my_proj<\/strong>\r\n<strong>cd my_proj\r\n\r\n<\/strong><\/pre>\n<h3 id=\"create-new-app\">Create a New App<\/h3>\n<p>From the Django project directory, we will create a new app called \u201cbooks\u201d to store our books collection:<\/p>\n<pre><strong>py manage.py startapp books\r\n\r\n<\/strong><\/pre>\n<p>We will also need to register the new app in our Django project, add the app \u201cbooks\u201d to the INSTALLED_APPS in your\u00a0<code>my_proj\/settings.py<\/code>:<\/p>\n<pre><strong>INSTALLED_APPS = (<\/strong>\r\n<strong> :<\/strong>\r\n<strong> 'student',<\/strong>\r\n<strong> :<\/strong>\r\n<strong> )\r\n<\/strong><\/pre>\n<h3 id=\"create-the-model\">Create the Model<\/h3>\n<p>The model file would be <code>student\/models.py<\/code>:<\/p>\n<pre>from django.db import models\r\n\r\nclass Student(models.Model):\r\n    rollno = models.AutoField(primary_key=True)\r\n    fullname = models.CharField(max_length=30)\r\n    address = models.CharField(max_length=30)\r\n    email = models.CharField(max_length=30)\r\n    age = models.IntegerField()\r\n    des = models.CharField(max_length=30)\r\n\r\nAfter defining the model, you need to provide it to the database:\r\n\r\n<\/pre>\n<pre><strong>py manage.py makemigrations <\/strong>\r\n<strong>py manage.py migrate\r\n<\/strong><\/pre>\n<h3 id=\"admin-interface-optional\">Admin Interface (Optional)<\/h3>\n<pre><strong>from django.contrib import admin<\/strong>\r\n<strong>from .models import student<\/strong>\r\n\r\n<strong>admin.site.register(student)\r\n<\/strong><\/pre>\n<h3 id=\"the-views\">The Views<\/h3>\n<p>We will use Django Class-based views to create our app pages, hence, the file <code>student\/views.py<\/code>\u00a0would look like:<\/p>\n<pre>from django.shortcuts import render,redirect\r\nfrom .models import Student\r\ndef SaveStudent(request):\r\n    if request.method=='GET':\r\n        return render(request,'index.html')\r\n    else:\r\n        fullname = request.POST['fulname']\r\n        address = request.POST['add']\r\n        email = request.POST['email']\r\n        age = request.POST['age']\r\n        designation = request.POST['des']\r\n        print(fullname, address, email, age, designation)\r\n\r\n#Method 1, it is commented\r\n        # st = Student()\r\n        # st.fullname = request.POST['fulname']\r\n        # st.address = request.POST['add']\r\n        # st.age = request.POST['age']\r\n        # st.email = request.POST['email']\r\n        # st.des = request.POST['des']\r\n        # st.save()\r\n#Method 2\r\n        stu = Student(fullname=fullname,address=address,email=email,age=age,des=designation)\r\n        stu.save()\r\n        return redirect('\/students\/list\/')\r\n\r\ndef showstudentslist(request):\r\n    all_students = Student.objects.all()\r\n    context = {\r\n        'students':all_students\r\n    }\r\n    return render(request, 'stulist.html', context)\r\n\r\ndef deletestudents(request,id):\r\n    print(id)\r\n    student = Student.objects.get(rollno=id)\r\n    student.delete()\r\n    return  redirect('\/students\/list\/')\r\n\r\ndef updatestudents(request,id):\r\n    student = Student.objects.get(rollno=id)\r\n    context = {\r\n        'roll':student.rollno,\r\n        'fn': student.fullname,\r\n        'add': student.address,\r\n        'em': student.email,\r\n        'age': student.age,\r\n        'des': student.des\r\n    }\r\n    return render(request, 'update.html', context)\r\n\r\ndef updateprocess(request):\r\n    rollno = request.POST['roll']\r\n    fullname = request.POST['fulname']\r\n    address = request.POST['add']\r\n    email = request.POST['email']\r\n    age = request.POST['age']\r\n    designation = request.POST['des']\r\n    print(fullname, address, email, age, designation)\r\n\r\n    st = Student()\r\n    st.rollno = request.POST['roll']\r\n    st.fullname = request.POST['fulname']\r\n    st.address = request.POST['add']\r\n    st.age = request.POST['age']\r\n    st.email = request.POST['email']\r\n    st.des = request.POST['des']\r\n    st.save()\r\n    return redirect('\/students\/list\/')<\/pre>\n<h3 id=\"define-the-urls\">Define the URLs<\/h3>\n<p>We need to define app URLs in the file\u00a0<code>student\/urls.py<\/code>\u00a0(create the file):<\/p>\n<pre>from django.urls import path\r\nfrom student import views\r\n\r\nurlpatterns = [\r\n    path('save\/', views.SaveStudent),\r\n    path('list\/', views.showstudentslist),\r\n    path('update\/&lt;int:id&gt;', views.updatestudents),\r\n    path('update\/', views.updateprocess),\r\n    path('delete\/&lt;int:id&gt;', views.deletestudents),\r\n]<\/pre>\n<h3 id=\"templates\">Templates<\/h3>\n<h3 id=\"templates\">For saving student details<\/h3>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n   &lt;head&gt;\r\n      &lt;meta charset=\"UTF-8\"&gt;\r\n      &lt;title&gt;Title&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n      &lt;h1&gt;Student Registration Form&lt;\/h1&gt;\r\n      &lt;form action=\"\/students\/save\/\" method=\"post\"&gt;\r\n         {% csrf_token %}\r\n         Full Name : &lt;input name=\"fulname\"&gt;&lt;br&gt;\r\n         Address : &lt;input name=\"add\"&gt;&lt;br&gt;\r\n         email : &lt;input name=\"email\"&gt;&lt;br&gt;\r\n         Age : &lt;input name=\"age\"&gt;&lt;br&gt;\r\n         Designation : &lt;input name=\"des\"&gt;&lt;br&gt;\r\n         &lt;input type=\"submit\" value=\"Save\"&gt;\r\n      &lt;\/form&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<h3 id=\"templates\">For Listing all student details<\/h3>\n<pre>{% load static %}\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n   &lt;head&gt;\r\n      &lt;meta charset=\"UTF-8\"&gt;\r\n      &lt;title&gt;Title&lt;\/title&gt;\r\n      &lt;link rel=\"stylesheet\" href=\"{% static 'style.css'%}\" type=\"text\/css\"&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n      &lt;h1 class=\"stu\"&gt;Student List&lt;\/h1&gt;\r\n      &lt;table border=\"1\"&gt;\r\n         &lt;tr&gt;\r\n            &lt;th&gt;Rollno&lt;\/th&gt;\r\n            &lt;th&gt;Fullname&lt;\/th&gt;\r\n            &lt;th&gt;Address&lt;\/th&gt;\r\n            &lt;th&gt;Email&lt;\/th&gt;\r\n            &lt;th&gt;Age&lt;\/th&gt;\r\n            &lt;th&gt;Action&lt;\/th&gt;\r\n         &lt;\/tr&gt;\r\n         {% for a in students %}\r\n         &lt;tr&gt;\r\n            &lt;td&gt;{{a.rollno}}&lt;\/td&gt;\r\n            &lt;td&gt;{{a.fullname}}&lt;\/td&gt;\r\n            &lt;td&gt;{{a.address}}&lt;\/td&gt;\r\n            &lt;td&gt;{{a.email}}&lt;\/td&gt;\r\n            &lt;td&gt;{{a.age}}&lt;\/td&gt;\r\n            &lt;td&gt;&lt;a href=\"\/students\/update\/{{a.rollno}}\"&gt;Update&lt;\/a&gt; |\r\n               &lt;a href=\"\/students\/delete\/{{a.rollno}}\"&gt;Delete&lt;\/a&gt;\r\n            &lt;\/td&gt;\r\n         &lt;\/tr&gt;\r\n         {% endfor %}\r\n      &lt;\/table&gt;\r\n      {{msg}}\r\n      &lt;a href=\"\/accounts\/logout\/\"&gt;logout&lt;\/a&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<h3 id=\"templates\">For Updating student details<\/h3>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n   &lt;head&gt;\r\n      &lt;meta charset=\"UTF-8\"&gt;\r\n      &lt;title&gt;Title&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n      &lt;h1&gt;Student Registration Form&lt;\/h1&gt;\r\n      &lt;form action=\"\/students\/update\/\" method=\"post\"&gt;\r\n         {% csrf_token %}\r\n         &lt;input type=\"hidden\" name=\"roll\" value=\"{{roll}}\"&gt;&lt;br&gt;\r\n         Full Name : &lt;input name=\"fulname\" value=\"{{fn}}\"&gt;&lt;br&gt;\r\n         Address : &lt;input name=\"add\" value=\"{{add}}\"&gt;&lt;br&gt;\r\n         email : &lt;input name=\"email\" value=\"{{em}}\"&gt;&lt;br&gt;\r\n         Age : &lt;input name=\"age\" value=\"{{age}}\"&gt;&lt;br&gt;\r\n         Designation : &lt;input name=\"des\" value=\"{{des}}\"&gt;&lt;br&gt;\r\n         &lt;input type=\"submit\" value=\"Save\"&gt;\r\n      &lt;\/form&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\nWe hope you enjoyed the blog. We will be happy to hear your feedback. please visit www.vytcdc.com<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Django is a free, open-source framework for web applications written in Python. Django offers a large assortment of modules you can use in your projects. Firstly, there are frameworks to save developers a lot of time and headaches wasted, and Django is no different. In this post, we briefly cover the steps needed to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3950,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Django Crud Web Application - TCDC<\/title>\n<link rel=\"canonical\" href=\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Django Crud Web Application - TCDC\" \/>\n<meta property=\"og:description\" content=\"Introduction Django is a free, open-source framework for web applications written in Python. Django offers a large assortment of modules you can use in your projects. Firstly, there are frameworks to save developers a lot of time and headaches wasted, and Django is no different. In this post, we briefly cover the steps needed to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/\" \/>\n<meta property=\"og:site_name\" content=\"TCDC\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vytcdc\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-24T11:59:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-10T15:19:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cns72.com\/vytcdc.com.sg\/wp-content\/uploads\/2020\/11\/mysql.png\" \/>\n\t<meta property=\"og:image:width\" content=\"710\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vytcdc\" \/>\n<meta name=\"twitter:site\" content=\"@vytcdc\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"5 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/#website\",\"url\":\"https:\/\/cns72.com\/vytcdc.com.sg\/\",\"name\":\"TCDC\",\"description\":\"Career Development Courses\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cns72.com\/vytcdc.com.sg\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/cns72.com\/vytcdc.com.sg\/wp-content\/uploads\/2020\/11\/mysql.png\",\"width\":710,\"height\":540,\"caption\":\"python django crud\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/#webpage\",\"url\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/\",\"name\":\"Python Django Crud Web Application - TCDC\",\"isPartOf\":{\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/#primaryimage\"},\"datePublished\":\"2020-11-24T11:59:37+00:00\",\"dateModified\":\"2020-12-10T15:19:11+00:00\",\"author\":{\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/#\/schema\/person\/c57e5f7b91685a93f23a57aaafd38e82\"},\"breadcrumb\":{\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/\",\"url\":\"https:\/\/cns72.com\/vytcdc.com.sg\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/\",\"url\":\"https:\/\/cns72.com\/vytcdc.com.sg\/python-django-crud-web-application\/\",\"name\":\"Python Django Crud Web Application\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/#\/schema\/person\/c57e5f7b91685a93f23a57aaafd38e82\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cns72.com\/vytcdc.com.sg\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f8f959f70994a4401c8704d6b2143474?s=96&d=mm&r=g\",\"caption\":\"admin\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/posts\/3949"}],"collection":[{"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/comments?post=3949"}],"version-history":[{"count":0,"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/posts\/3949\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/media\/3950"}],"wp:attachment":[{"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/media?parent=3949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/categories?post=3949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cns72.com\/vytcdc.com.sg\/wp-json\/wp\/v2\/tags?post=3949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}