So i am new to Django, and i want to make some hardware compatibility check feature in my app, i make a form to be filled and check the compatibility from database then show me the result. but the problem is the form seems to just keep saving to database without processing the logical operation first. this is my view
class CreateSimView(LoginRequiredMixin, CreateView):
login_url = '/login/'
model = Simulation
form_class = SimPostForm
template_name = 'sim_post.html'
redirect_field_name = 'sim/sim.html'
def simpost(request):
mtb = Motherboard.objects.all()
cpu = Cpu.objects.all()
vga = Vga.objects.all()
ram = Ram.objects.all()
storage = Storage.objects.all()
mtbform = request.GET.get('mtb_name')
cpuform = request.GET.get('cpu_name')
vgaform = request.GET.get('vga_name')
ramform = request.GET.get('ram_name')
strform = request.GET.get('str_name')
simcpu = cpu.objects.values_list('socket', flat=True).filter(name__icontains=cpuform)
simcpu1 = mtb.objects.values_list('socket', flat=True).filter(name__icontains=mtbform)
simvga = vga.objects.values_list('vga_interface', flat=True).filter(name__icontains=vgaform)
simvga1 = mtb.objects.values_list('vga_interface', flat=True).filter(name__icontains=mtbform)
simram = ram.objects.values_list('mem_type', flat=True).filter(name__icontains=ramform)
simram1 = mtb.objects.values_list('mem_type', flat=True).filter(name__icontains=mtbform)
simstr = str.objects.values_list('str_interface', flat=True).filter(name__icontains=strform)
simstr1 = mtb.objects.values_list('str_interface', flat=True).filter(name__icontains=mtbform)
if simcpu == simcpu1 :
if simvga == simvga1:
if simram == simram1:
if simstr == simstr1:
form = SimPostForm(request.POST)
if form.is_valid():
form.save()
return render(mtbform,cpuform,vgaform,ramform,strform,"/")
else:
strform = "not compatible"
return render(mtbform,cpuform,vgaform,ramform,strform,"/")
else:
ramform = "not compatible"
return render(mtbform,cpuform,vgaform,ramform,strform,"/")
else:
vgaform = "not compatible"
return render(mtbform,cpuform,vgaform,ramform,strform,"/")
else:
cpuform = "not compatible"
return render(mtbform,cpuform,vgaform,ramform,strform,"/")
my models for the form
class Simulation(models.Model):
build_name = models.CharField(blank=False, max_length=150)
mtb_name = models.CharField(blank=False, max_length=150)
cpu_name = models.CharField(blank=False, max_length=150)
vga_name = models.CharField(blank=False, max_length=150)
ram_name = models.CharField(blank=False, max_length=150)
str_name = models.CharField(blank=False, max_length=150)
def __str__(self):
return self.build_name
def get_absolute_url(self):
return reverse('sim:sim_post')
and my template
{% extends "base.html" %}
{% load bootstrap4 %}
{% block content %}
<div class="container">
<div class="jumbotron">
<h1>Pilih Parts</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<label for="build">Nama Rakitan</label>
<input class="form-control" type="text" id="build" name="build_name" value="{{ form.build_name.value|default_if_none:"" }}">
</div>
<div class="form-group">
<label for="mtb">Motherboard</label>
<input class="form-control" id="mtb" type="text" name="mtb_name" value="{{ form.mtb_name.value|default_if_none:"" }}">
</div>
<div class="form-group">
<label for="vga">Vga</label>
<input class="form-control" id="vga" type="text" name="vga_name" value="{{ form.vga_name.value|default_if_none:"" }}">
</div>
<div class="form-group">
<label for="ram">Ram</label>
<input class="form-control" type="text" id="ram" name="ram_name" value="{{ form.ram_name.value|default_if_none:"" }}">
</div>
<div class="form-group">
<label for="storage">Storage</label>
<input class="form-control" type="text" id="storage" name="str_name" value="{{ form.storage_name.value|default_if_none:"" }}">
</div>
<div class="form-group">
<label for="cpu">Cpu</label>
<input class="form-control" type="text" id="cpu" name="cpu_name" value="{{ form.cpu_name.value|default_if_none:"" }}">
</div>
<button type="submit" class="btn btn-secondary">Cek Kompatibilitas</button>
</form>
</div>
</div>
{% endblock %}
i also use {{ form.storage_name.value|default_if_none:"" }}
in my form hoping it would return the value from before. but it doesn't seem to do that.
It seems to me that you implemented your logic in
def simpost(request)
but you missed to integrate the call of this function in the predefined processing of the CreateView.
So one approach might be to override
def post(self, request, *args, **kwargs)
of the CreateView and put your logic there. If everything is fine, you should call
super().form_valid(your_form_object_here)
to trigger the creation of your data with the predefined actions of the CreateView.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.