Skip to content

Instantly share code, notes, and snippets.

View aballah-chamakh's full-sized avatar

abdallah-chamakh aballah-chamakh

View GitHub Profile
from django.db import models
class Porduct(models.Model):
title = models.CharField(max_length=255)
image = models.ImageField()
price = models.IntergerField()
def __str__(self):
return self.title
from django.contrib import admin
from .models import Product
# Register your models here.
admin.site.register(Product)
from django.shortcuts import render
from .models import Product
from .forms import PorductForm
# function based views
def product_detail_view(request,id):
product_obj = Product.objects.get(id=id)
context = {'product_obj':product_obj}
return render(request,'product_detail.html',context)
from django.shortcuts import render
from django.views.generic import ListView,DetailView,CreateView,UpdateView,DeleteView
from .models import Product
from .forms import ProductForm
class ProductListView(ListView):
queryset = Product.objects.all()
template_name = 'product_view.html'
from djanogo import forms
from .models import Porduct
class PorductForm(forms.ModelForm):
class Meta :
model = Porduct
fields = ['title','image','price']
from django.urls import path
# import function based view
from .views import (product_detail_view,product_list_view,product_create_view,product_update_view,product_delete_view)
# or class based views (this up to what your choise either you will work with cbv or fbv)
from .view import (ProductListView,ProductDetaiView,ProductCreateView,ProductUpdateView,ProductDeleteView)
urlpatterns = [
# for function based view
path('',product_list_view)
from django.urls import path
from django.conf.urls import include
from Product import urls as product_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include(product_urls))
]
<html>
<head>
<title>Product list</tile>
</head>
<body>
{% for object in objects %}
<div>
<h3>{{object.title}}</h3>
<img src='{{object.image.url}}' />
<h5> price {{object.price}} </h5>
<html>
<head>
<title>{{object.title}}</title>
</head>
<body>
<div>
<h3>{{object.title}}</h3>
<img src='{{object.image.url}}' />
<h5>price {{object.price}}</h5>
</div>
<html>
<head>
<title>create Product</title>
</head>
<body>
<div>
<form method='POST' enctype="multipart/form-data"> {% csrf_token %}
{{form.media}}
{{form.as_p}}
<input type="submit" value="create" >