Sunday, May 24, 2020

Issue with CRUD Operation

{% extends "base.html" %} {% load crispy_forms_tags %} {% block content %}
{% csrf_token %}
{{form.date|as_crispy_field}}
{{form.Serial_White_Guava|as_crispy_field}}
{{form.Serial_White_Premium|as_crispy_field}}
{{form.Soft_Flower_Body_Wash|as_crispy_field}}
{{form.Soft_Flower_Fresh|as_crispy_field}}
{{form.Soft_Flower_Orange_Lightening|as_crispy_field}}
{{form.Soft_Flower_Carot_Lightening|as_crispy_field}}
{{form.Soft_Flower_Papaya_Extract|as_crispy_field}}
{{form.Soft_Flower_Extra_Lightening|as_crispy_field}}
{{form.Serial_White_Papaya_4in1|as_crispy_field}}
{% endblock content %}# from django.shortcuts import render
# from .models import Postrec
# from .forms import ProductForm

# # Create your views here.

# # Create your views here.
# from django.views.generic import TemplateView

# class HomePageView (TemplateView):
# template_name = 'home.html'
# class AboutPageView (TemplateView):
# template_name = 'about.html'
# #from django.shortcuts import render
# class CreatepostPageView (TemplateView):
# template_name = 'createpost.html'

from django.http import HttpResponseRedirect
# from django.shortcuts import redirect
from django.shortcuts import render
from .models import Soapprod
from .forms import StaffrecForm, SoapprodForm
from django.views.generic import TemplateView

# def HomePageView (request, *args, **kwargs):
# return render(request,"home.html", {})
def Basep_view (request,*args, **kwargs):
return render(request,"basep.html", {})
def Namev_view (request,*args, **kwargs):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
data = Soapprod(serial_white_guava ='Me')
form = NameForm(request.POST, instance=data)
form.save()
return HttpResponseRedirect('/namev/')
else:
form = NameForm()
sum = 10/2
return render(request, "namev.html", {'form': form,'sum':sum})

# pages/views.py
# from django.shortcuts import render
from pages.forms import UserForm,UserProfileInfoForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required
def index(request):
my_name = "My name is Chielo Celestine"
from pages.my_name import name

return render(request,'index.html',{'ur_name':my_name,'another':name})
@login_required
def special(request):
return HttpResponse("You are logged in !")
@login_required
def user_logout (request):
logout (request)
return HttpResponseRedirect(reverse('index'))
def register (request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
print('found it')
profile.profile_pic = request.FILES['profile_pic']
profile.save()
registered = True
else:
print(user_form.errors,profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileInfoForm()
return render(request,'registration.html',
{'user_form':user_form,
'profile_form':profile_form,
'registered':registered})
def user_login (request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request,user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("Your account was inactive.")
else:
print("Someone tried to login and failed.")
print("They used username: {} and password: {}".format(username,password))
return HttpResponse("Invalid login details given")
else:
return render(request, 'login.html', {})

from django.shortcuts import render

# Create your views here.
from django.http import JsonResponse
from pages.models import Soapprod
from django.core import serializers

def dashboard_with_pivot (request):
return render(request, 'dashboard_with_pivot.html', {})
def pivot_data(request):
dataset = Soapprod.objects.all()
data = serializers.serialize('json', dataset)
return JsonResponse(data, safe=False)


# delete view for details

def soapprod_list (request):
context = {'soapprod_list': Soapprod.objects.all()}
return render(request, "soapprod_list.html", context)

def soapprod_form (request, id=0):
if request.method == "GET":
if id == 0:
form = SoapprodForm()
else:
soapprod = Soapprod.objects.get(pk=id)
form = SoapprodForm (instance=soapprod)
return render(request, "soapprod_form.html", {'form': form})
else:
if id == 0:
form = SoapprodForm(request.POST)
else:
soapprod = Soapprod.objects.get(pk=id)
form = SoapprodForm(request.POST,instance= soapprod)
if form.is_valid():
form.save()
return render(request, "soapprod_form.html", {'form': form})
#return render(request,'/employee/list')
#return render(request,'soapprod_list.html')
#return render(request, "soapprod_form.html", {'form': form})
#return redirect(request, "soapprod_form.html", {'form': form})
#return render(request,'soapprod_list.html')
#return redirect('/employee/list')

def soapprod_delete (request,id):
soapprod = Soapprod.objects.get(pk=id)
if request.method =="POST":
soapprod.delete()
return ('/soapprod/lists')
#return render(request, "soapprod_form.html", {'form': form})
# return redirect('/soapprod/list')
# Staff INFORMATION
from django.shortcuts import render, redirect
from pages.forms import StaffrecForm
from .models import Staffrec

from django.db import models
from datetime import date
class Employee(models.Model):
fullname = models.CharField(max_length=100)
emp_code = models.CharField(max_length=3)
mobile= models.CharField(max_length=15)
position= models.CharField(max_length=15)

class Staffrec(models.Model):
staff_id = models.CharField(max_length=5)
fullname = models.CharField(max_length=35)
sex = models.CharField(max_length=7)
email = models.EmailField()
address = models.CharField(max_length=100)
mobile= models.CharField(max_length=15)
position= models.CharField(max_length=30)
department = models.CharField(max_length=25)
class Meta:
db_table = "staffrec"

class Soapprod (models.Model):
id = models.AutoField(primary_key=True)
Serial_White_Guava = models.CharField(max_length=7, blank = True, null = True)
Serial_White_Premium = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_Body_Wash = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_Fresh = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_Orange_Lightening = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_Carot_Lightening = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_Papaya_Extract = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_Extra_Lightening = models.CharField(max_length=7, blank = True, null = True)
Serial_White_Papaya_4in1 = models.CharField(max_length=7, blank = True, null = True)
date = models.DateField(default = date.today)

def __str__(self):
return self.serial_white_guava

class Creamprod (models.Model):
id = models.AutoField(primary_key=True)
Serial_White_ExLM = models.CharField(max_length=7, blank = True, null = True)
Serial_White_Gold_SWM = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_LL = models.CharField(max_length=7, blank = True, null = True)
Soft_Flower_FC = models.CharField(max_length=7, blank = True, null = True)
date = models.DateField(default = date.today)


from django.contrib.auth.models import User
# Create your models here.
class UserProfileInfo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
portfolio_site = models.URLField(blank=True)
profile_pic = models.ImageField(upload_to='profile_pics',blank=True)
def __str__(self):
return self.user



# class Soapsales (Soapprod):
# SOAP_MARKETER_STATUS = (
# ('M', 'Marketers'),
# ('D1', 'Distributor 1'),
# ('D2', 'Distributor 2'),
# ('D3', 'Distributor 3'),
# )
# soap_marketer_status = models.CharField(max_length=1, choices=SOAP_MARKETER_STATUS)

class Creamsales (Creamprod):
CREAM_MARKETER_STATUS = (
('M', 'Marketers'),
('D1', 'Distributor 1'),
('D2', 'Distributor 2'),
('D3', 'Distributor 3'),
)
cream_marketer_status = models.CharField(max_length=1, choices=CREAM_MARKETER_STATUS)

class Staffinfo (models.Model):
staff_id = models.CharField(max_length=5)
Hello,
I performed a CRUD operation and every thing is working well except two things
1. my insert form does not clear the text boxes after saving a record
2. my delete operation renders an error page after deleting a record with a message ''DoesNotExist at /delete/3/" or "AttributeError at /delete/6/" 
example of the error messages are  shown below. i also attached view.py, model.py and template file

DoesNotExist at /delete/3/

Soapprod matching query does not exist.

Request Method:

POST

Request URL:

http://localhost:8000/delete/3/

Django Version:

2.1.5

Exception Type:

DoesNotExist

Exception Value:

Soapprod matching query does not exist.

Exception Location:

C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py in get, line 399

Python Executable:

C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\python.exe

Python Version:

3.7.2

Python Path:

['C:\\Users\\IFEANYI CHIELO\\divinecrown',

 'C:\\Users\\IFEANYI '

 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',

 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',

 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib',

 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37',

 'C:\\Users\\IFEANYI '

 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

Server time:

Mon, 25 May 2020 00:03:25 +0000


or


AttributeError at /delete/6/

'str' object has no attribute 'get'

Request Method:

POST

Request URL:

http://localhost:8000/delete/6/

Django Version:

2.1.5

Exception Type:

AttributeError

Exception Value:

'str' object has no attribute 'get'

Exception Location:

C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\middleware\clickjacking.py in process_response, line 26

Python Executable:

C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\python.exe

Python Version:

3.7.2

Python Path:

['C:\\Users\\IFEANYI CHIELO\\divinecrown',

 'C:\\Users\\IFEANYI '

 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',

 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',

 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib',

 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37',

 'C:\\Users\\IFEANYI '

 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

Server time:

Mon, 25 May 2020 00:09:23 +0000

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b43b280d-ab19-4620-8544-133c897396f0%40googlegroups.com.

No comments:

Post a Comment