Django is a secure and easy-to-learn web framework which follows MVT architecture inspired by MVC(Model-View-Controller) enabling us a quick development process with its built-in admin side, ORM(Object Relational Mapping), etc.
MVT here is Model-View-Template
The model is used to create and apply constraints on Tables/Collections in the Database. It looks like the image below.
class Orders(models.Model): order_id = models.AutoField(primary_key=True) user_id = models.ForeignKey(User, on_delete=models.CASCADE,default=None) product_ids = models.CharField(max_length=200,default=None) address_id = models.ForeignKey(Useraddress,on_delete=models.CASCADE,default = None) price = models.IntegerField() deliverycharges = models.IntegerField(null=True) orderdate = models.DateTimeField(auto_now_add=True)
The Views are functions or classes which accept HTTP requests and can process the request if needed and returns an HTTP response which may contain HTML files, HTTPResponse, JsonResponse, etc.
def viewCart(request):
user = User.objects.get(uid = request.user.uid)
cart = Cart.objects.filter(uid=user)
if not cart:
messages.error(request,"No Products found")
context = {'cart' : cart}
return render(request, 'customer/cart.html' ,context=context)
- The template is where the Whole HTML code is written and can be manipulated using Built-in Django Templating Engine
#Base.html which will be inherited in all the other html files so that you don't have to rewrite the same code over and over.
<!doctype html>
<html lang="eng">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ecommerce | {% block title %}{% endblock title %}</title>
{% block head %}
{% endblock head %}
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>
#This file inherits base.html and demonstrates use of block, include and for tags.
{% extends 'customer/base.html' %}
{% block title %}
Cart
{% endblock title %}
{% block content %}
{% include 'customer/navbar.html' %}
<div class="container mt-5" >
<div class="list-group">
{% for product in cart %}
<span class="list-group-item list-group-item-action mb-3">
<a href="productdetails?id={{product.pid.pid}}"> {{product.pid.name}} </a>
<a class="btn btn-sm btn-danger float-end" href="removefromcart?id={{product.pid.pid}}">Remove from Cart</a>
</span>
{% endfor %}
</div>
<div>
<a href="checkout/address?source=cart" class="btn btn-success ">Checkout</a>
</div>
</div>
{% endblock content %}
The controller in Django is handled by Django itself.
That's it for Django's Introduction. Please keep an eye on my blogs for similar Django-related blogs with in-depth knowledge.
I am passionate about web development and have extensive experience using Django to build web applications. I believe I have the skills and knowledge to be an effective Junior Django Developer for your company. I am confident that I can provide quality work and make a positive contribution to your team. Therefore, If you would like to know more about my experience and qualifications, please do not hesitate to contact me via email at rajm150503@gmail.com.
(Remote jobs preferred, Relocation possible situationally)
If you want me to write Django, DRF or web dev-related blogs for you, please contact me via email at rajm150503@gmail.com.