Django 2 CRUD Tutorial: Generic Class-Based Views
In this post we will give you information about Django 2 CRUD Tutorial: Generic Class-Based Views. Hear we will give you detail about Django 2 CRUD Tutorial: Generic Class-Based ViewsAnd how to use it also give you demo for it if it is necessary.
Throughout this tutorial, you’ll create a Django 2 CRUD example application using generic class based views. You will be using the latest Python 3.7 version.
You’ll learn to quickly create CRUD views by using the Django built-in generic views such as ListView, DetailView, CreateView, UpdateView and DeleteView.
CRUD stands for create, read, update and delete and it simply refers to a set of common operations that are used in most web applications to interact with data from a database. It basically provides a CRUD interface that allows your users to create, read, update and delete data in your application database like MySQL, PostgreSQL or Oracle etc.
Prerequisites
You’ll need to have the following prerequisites:
- A development machine with Python 3.7,
pipandvenvinstalled, - A basic knowledge of Python,
- A working knowledge of Django.
In this tutorial, you’ll learn about:
- Django class based views,
- Creating and activating a virtual environment using
venv, - Installing Django using
pipand creating a new project usingdjango-admin.py, - Creating a new Django application using
manage.py, - Creating a Django ORM model,
- Enabling the admin Interface,
- Creating the class based views,
- Creating the templates,
- Adding the URLs.
Creating and Activating a Virtual Environment
When working with new Python projects, it’s recommended that you create a virtual and isolated environment for your project’s packages. In Python 3.7, you can use the venv module to create virtual environments.
Go to your terminal and run the following command:
$ python -m venv envA virtual environment called env is created.
Next, you’ll need to activate the environment using the source command:
$ source env/bin/activateNow, you can install your project’s dependencies without worrying about interfering with the other projects.
Installing Django and Creating a New Project
Let’s install Django 2 into our virtual environment using pip:
$ pip install djangoNext, create a new project using django-admin.py:
$ django-admin.py startproject django_crud_cbvCreating a New Application
Now, let’s create a new application in our Django project. Navigate inside your project’s folder and run the following command:
$ cd django_crud_cbv$ python manage.py startapp contactsThis will create an application called contacts.
You need to add this application to your project. Open the settings.py file and add contacts to the INSTALLED_APPS array:
INSTALLED_APPS=(# [...]'contacts',)Creating a Model and Migrating the Database
After creating the contacts application, you’ll need to a model that represents contacts in the database. Open the contacts/models.py file and add the following code:
fromdjango.contrib.gis.dbimportmodelsclassContact(models.Model):name=models.CharField(max_length=100)email=models.EmailField()address=models.CharField(max_length=100)phone=models.CharField(max_length=50)Now, that you have defined your model, you can migrate your database and run your development server.
Go to your terminal and run these commands:
$ python manage.py makemigrations$ python manage.py migrateEnabling the Admin Interface
At this point of your project, you can already have a CRUD interface by registering your model with the admin application with isa built-in Django application that comes with every project.
Open the contacts/admin.py file and register the Contact model:
fromdjango.contribimportadminfrom.modelsimportContactadmin.site.register(Contact)You can access the admin interface from http://localhost:8000/admin.
Creating the CRUD Views
Instead of using the admin interface to perform CRUD operations against our database, let’s create our own CRUD views.
We’ll be using the Django class based generic views to define our views. Open the contacts/views.py file and start by adding the following imports:
fromdjango.views.genericimportListView,DetailViewfromdjango.views.generic.editimportCreateView,UpdateView,DeleteViewfrom.modelsimportContactNow, you can add a first view for listing the contacts using the ListView generic view:
classContactList(ListView):model=ContactNext, create a detail view using the DetailView generic view class:
classContactDetail(DetailView):model=ContactNext, you need to add the create view for creating contacts using the CreateView generic view:
classContactCreate(CreateView):model=ContactNext, you need to add the update view for updating contacts using the UpdateView view:
classContactUpdate(UpdateView):model=ContactFinally, you need to add the delete view for deleting contacts using the DeleteView class-based generic view:
classContactDelete(DeleteView):model=ContactAdding the Templates
After defining the CRUD views, you next need to add the template for each of your views. Each view expects a template with a specific name in the templates folder of your application.
Inside the contacts folder, create a templates/contacts/ folder and start by adding the contact_list.html file with the following content:
<h1>Contacts</h1><table><thead><tr><th>Name</th><th>Email</th><th>Address</th><th>Phone</th><th>Actions</th></tr></thead><tbody>{% for contact in object_list %}<tr><td>{{ contact.name }}</td><td>{{ contact.email }}</td><td>{{ contact.address }}</td><td><ahref="{% url "contact_detail"contact.id%}">details</a><ahref="{% url "contact_edit"contact.id%}">edit</a><ahref="{% url "contact_delete"contact.id%}">delete</a></td></tr>{% endfor %}</tbody></table>Next, create the contact_details.html file with the following content:
<h1>Contact Details</h1>Name: {{object.name}}</p>Email: {{object.email}}</p>Address: {{object.address}}</p>Phone: {{object.phone}}</p>Next, let’s create the contact_form.html file that will be used by the update view:
<h1>Contact Update</h1><formmethod="post">{% csrf_token %}{{ form.as_p }}<inputtype="submit"value="Submit"/></form>Finally you need to create the contact_confirm_delete.html file that will be used by the delete view:
<h1>Contact Delete?</h1><formmethod="post">{% csrf_token %}Are you sure you want to delete this contact?<inputtype="submit"value="Submit"/></form>Adding the URLs
Finally, you need to add various URLs to the views you have defined. Open the urls.py file of your project and add the following URLs:
fromdjango.urlsimportpathfrom.importviewsurlpatterns=[# [...]path('contacts',views.ContactList.as_view(),name='contact_list'),path('contact/<int:pk>',views.ContactDetail.as_view(),name='contact_detail'),path('create',views.ContactCreate.as_view(),name='contact_create'),path('update/<int:pk>',views.ContactUpdate.as_view(),name='contact_update'),path('delete/<int:pk>',views.ContactDelete.as_view(),name='contact_delete'),]Finally, you can you run development server using:
$ python manage.py runserverYou can then access your CRUD interface from http://localhost:8000/contacts/
Conclusion
In this tutorial, you have created a CRUD project with Django and Python 3.7.
You have used the various class-based generic views provided by Django such as ListView, DetailView, CreateView, UpdateView and DeleteView to create CRUD views that allow your users to create, read, update and deete contacts from your database. In the example, we have used SQLite but you can very easily switch to PostgreSQL, MySQL or any database you want without changing anything in your code thanks to Django ORM.
Hope this code and post will helped you for implement Django 2 CRUD Tutorial: Generic Class-Based Views. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
