def test_view2(request, motordb_pk=None): form = forms.MotorsForm() if not motordb_pk: if request.method == 'POST': form = forms.MotorsForm(request.POST) if form.is_valid(): form.save(commit=True) return motordb_index(request) else: print('form not valid') if motordb_pk: motor = models.Motors.objects.get(pk=motordb_pk) form = forms.MotorsForm(instance=motor) form.save() return render(request, 'motordb/form_page.html', {'form': form})
def motordb_edit(request, motordb_pk): motor_instance = models.Motors.objects.get(pk=motordb_pk) form = forms.MotorsForm(request.POST or None, instance=motor_instance) if form.is_valid(): form.save(commit=True) return motordb_index(request) else: print('error: form invalid')
def motordb_edit_form(request, motordb_pk): # edit = input('do you want to edit this ? (Y/N)') # if edit == 'Y': # motor = models.Motors.objects.get(pk=motordb_pk) motor = models.MotorsCode.objects.get(pk=motordb_pk) form = forms.MotorsForm(instance=motor) return render(request, 'motordb/form_page_edit.html', { 'form': form, 'motor': motor })
def motordb_search_form(request): filter_items = ('kw', 'speed', 'voltage') kwargs = {} if request.POST: for filter in filter_items: if request.POST[filter]: kwargs[filter] = request.POST[filter] motor = models.Motors.objects.all() motor_instance = forms.MotorsForm() return render(request, 'motordb/search.html', { 'motors': motor, 'motor_instance': motor_instance, })
def motordb_search(request): filter_items = ('kw', 'speed', 'voltage') kwargs = {} motor_instance = forms.MotorsForm() for filter in filter_items: if request.POST[filter]: kwargs[filter] = request.POST[filter] motor_instance.fields[filter].initial = request.POST[filter] else: motor_instance.fields[filter].initial = None motors = models.Motors.objects.filter(**kwargs) # motor_instance = forms.SearchForm() context = { 'motors': motors, 'motor_instance': motor_instance, } return render(request, 'motordb/search.html', context)