Skip to content

Instantly share code, notes, and snippets.

@Sultan91
Created July 14, 2020 10:16
Show Gist options
  • Save Sultan91/0f375f05e4f77116d5e43d8fff4a66fd to your computer and use it in GitHub Desktop.
Save Sultan91/0f375f05e4f77116d5e43d8fff4a66fd to your computer and use it in GitHub Desktop.
from django.shortcuts import render, get_list_or_404, redirect
from .models import Product
from .forms import ProductForm, RawProductForm
from django.http import Http404
# Create your views here.
'''
def product_create_view(request):
form = ProductForm(request.POST or None)
if form.is_valid():
form.save()
print("Form got created")
form = ProductForm()
context = {
'form': form
}
return render(request, "products/product_create.html", context)
'''
def product_create_view(request):
my_form = RawProductForm()
if request.method == "POST":
my_form = RawProductForm(request.POST)
if my_form.is_valid():
print(my_form.cleaned_data)
Product.objects.create(**my_form.cleaned_data)
else:
print(my_form.errors)
context = {
'form': my_form
}
return render(request, "products/product_create.html", context)
def product_detailed_view(request):
obj = Product.objects.get(id=1)
context = {
'title': obj.title,
'description': obj.description
}
return render(request, "products/detail.html", context)
def dynamic_lookup_view(request, id):
try:
obj = Product.objects.get(id=id)
except Product.DoesNotExist:
raise Http404
context = {
'object': obj
}
return render(request, "products/product_detail.html", context)
def product_delete_view(request, id):
try:
obj = Product.objects.get(id=id)
except Product.DoesNotExist:
raise Http404
# Post request
print(obj)
if request.method == "POST":
obj.delete()
obj.__delitem__(id)
print(obj.title)
return redirect('/')
context = {
'object': obj
}
return render(request, "products/product_delete.html", context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment