Django is a powerful framework for building web applications in Python. In this tutorial, we will walk through the process of creating a CRM (Customer Relationship Management) app using Django.
Step 1: Setting up the Django Project
First, make sure you have Django installed on your machine. You can install Django using pip:
pip install django
Next, create a new Django project by running the following command in your terminal:
django-admin startproject crmapp
This will create a new directory called crmapp
with the basic structure for a Django project.
Step 2: Creating a Django App
Next, we need to create a Django app within our project. This app will contain the models, views, and templates for our CRM app.
Navigate to the crmapp
directory and run the following command to create a new app:
python manage.py startapp customers
This will create a new directory called customers
with the necessary files for a Django app.
Step 3: Creating Models
In our CRM app, we will need a Customer
model to store information about our customers. Open the models.py
file in the customers
app directory and define the Customer
model:
from django.db import models
class Customer(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
phone = models.CharField(max_length=15)
address = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
Step 4: Registering Models
Next, we need to register our Customer
model with the Django admin interface. Open the admin.py
file in the customers
app directory and register the Customer
model:
from django.contrib import admin
from .models import Customer
admin.site.register(Customer)
Step 5: Setting up the Database
Before we can use our models, we need to create and apply migrations to set up the database schema. Run the following commands in your terminal:
python manage.py makemigrations
python manage.py migrate
This will create the necessary database tables for our models.
Step 6: Creating Views
Now, we need to create views to display our customer data. Open the views.py
file in the customers
app directory and define the views:
from django.shortcuts import render
from .models import Customer
def customer_list(request):
customers = Customer.objects.all()
return render(request, 'customers/customer_list.html', {'customers': customers})
Step 7: Creating Templates
Next, we need to create HTML templates to display our customer data. Create a new directory called templates
in the customers
app directory and create a new HTML file called customer_list.html
:
<!DOCTYPE html>
<html>
<head>
<title>Customer List</title>
</head>
<body>
<h1>Customer List</h1>
<ul>
{% for customer in customers %}
<li>{{ customer.name }} - {{ customer.email }}</li>
{% endfor %}
</ul>
</body>
</html>
Step 8: Creating URLs
Finally, we need to create URLs to access our views. Open the urls.py
file in the customers
app directory and define the URL patterns:
from django.urls import path
from .views import customer_list
urlpatterns = [
path('', customer_list, name='customer_list'),
]
Step 9: Adding URLs to the Project
To make our app accessible, we need to include the URL patterns in the project’s urls.py
file. Open the urls.py
file in the crmapp
directory and include the app’s URLs:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('customers/', include('customers.urls')),
]
Step 10: Running the Django Server
Finally, we can run the Django development server to view our CRM app. Run the following command in your terminal:
python manage.py runserver
Navigate to http://localhost:8000/customers/
in your web browser to see the list of customers stored in the database.
Congratulations! You have successfully created a CRM app using Django. Feel free to customize and extend the app further with additional features and functionality.
Thanks for watching my video! For more Django courses check out my Youtube Channel:
https://www.youtube.com/@codemycom/
Or My Website: https://Codemy.com
you do things that are required to know after doing things that require knowing them before operating, this just confuses me on and on and i really don't seem to understand anything until i fast forward some 30 minutes or so, like how can you authenticate users and their passwords when you litterally didn't even make the database or the classes for them, so so so confusing, so not recommended to watch
great videos, but there is a lot of obscurities, i know Django is a high level framework but even with a fast and quick explanation of each step wouldn't harm, i know it sounds time consuming, but it isnt, thanks anyway
Very thanks.
def delete_record(request, pk):
if request.user.is_authenticated:
delete_it = Record.objects.get(id=pk)
first_name = delete_it.first_name
last_name = delete_it.last_name
delete_it.delete()
messages.success(request, f"Record for {first_name} {last_name} successfully deleted!")
return redirect('home')
else:
messages.success(request, ("You must be logged in to do that…"))
return redirect('home')
def delete_record(request, pk):
if request.user.is_authenticated:
delete_it = Record.objects.get(id=pk)
first_name = delete_it.first_name
last_name = delete_it.last_name
delete_it.delete()
messages.success(request, f"Record for {first_name} {last_name} successfully deleted!")
return redirect('home')
else:
messages.success(request, ("You must be logged in to do that…"))
return redirect('home')
thanks a lot
Amazin tutorial! Straight to the point 🙂
What would be the process of making one of the CharFields on the Add Records form a drop down box with a list of values?
Nice tutorial, this gave me all the mindset and details I needed about Django, thanks
Let's suppose I have a crud for products, suppliers, customers, etc. I create a new app for each one? like you did with the website? thanks for the tutorial!
Excellent tutorial! I learned a lot, thank you!!
It was fun to build that app! Pretty good initial point for upgrading it with more things!
Amazing
Our call center has experienced VAs ready to start in the best price you could get!!
thank you very much
Your creating a virtual environment on Mac not windows you should mention it
For some reason, the styling on my register.html form doesn't work properly. Only the First name and Last Name fields look like they're styled properly
Great tutorial. Thanks! BTW does Mary Smith really exist?
John, as usual, EXCELLENT Thanks so much 🙂