Django Functional Views - 2
Views:
from django.shortcuts import render,HttpResponse
from .models import Product
def product_list(request):
products = Product.objects.all()
context = {
"products":products
}
return render(request,"index.html",context)
Models:
from django.db import models
class Product(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
price = models.PositiveIntegerField()
Urls:
from django.contrib import admin
from django.urls import path
from products.views import product_list
urlpatterns = [
path('admin/', admin.site.urls),
path("products/",product_list)
]
Template:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for product in products %}
{% if product.price == 2332434 %}
asdfasdfasdfadf
{% endif %}
{% endfor %}
</body>
</html>
Comments
There are no comments yet.