How to insert Checkbox in django - python

hallo i want to create data with checkbox field but data cannot save in database when i use widget RadioSelect
this is forms.py
class VehicleAttribute(forms.ModelForm):
OPERATION = [('production','Production Vehicle'),('supporting','Supporting Vehicle')]
PAYLOAD_METER = [('yes','Yes'),('no','No')]
NUMBER_STRUT = [('0','0'),('3','3'),('4','4')]
operation = forms.ChoiceField(widget=forms.RadioSelect, choices = OPERATION)
payload_meter = forms.ChoiceField(widget=forms.RadioSelect, choices = PAYLOAD_METER)
number_of_strut_pressure = forms.ChoiceField(widget=forms.RadioSelect, choices = NUMBER_STRUT)
class Meta:
model = Vehicle_attribute
fields = ['operation','payload_meter','number_of_strut_pressure']
this is views.py
def data_vehicle_add(request):
if request.method == "POST":
form = VehicleAttribute(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('data_vehicle_add.html', pk=post.pk)
else:
form = VehicleAttribute()
return render(request,'data_vehicle_add.html', {'form':form}, context_instance=RequestContext(request))
can you help me solve this problem?

Related

Validating views.py

Can someone advice me how to prevent to 'POST' empty form? i think im missing: if form.is_valid()... but im too noob and dont know where to implement it...
def addContact(request):
form = ContactForm()
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# form = Contact(
# full_name = request.POST ('fullname'),
# relationship = request.POST ('relationship'),
# email = request.POST ('email'),
# phone_number = request.POST ('phone-number'),
# address = request.POST ('address'),
# )
form.save()
return redirect('/contact')
context = {'form': form}
return render(request, 'contact/new.html', context)
def contactProfile(request,pk):
contact = Contact.objects.get(id=pk)
return render(request, 'contact/contact-profile.html', {'contact': contact})
forms.py:
from django.forms import ModelForm
from .models import Contact
class ContactForm(ModelForm):
class Meta:
model = Contact
fields = '__all__'
models.py:
from django.db import models
# Create your models here.
class Contact(models.Model):
full_name = models.CharField(max_length=500)
relationship = models.CharField(max_length=50)
email = models.EmailField(max_length=254)
phone_number =models.CharField(max_length=20)
address = models.CharField(max_length=100)
def __str__(self):
return self.full_name
You were on the right track.
If you put the if form.is_valid(): inside your if request.method == 'POST'
then it should do what you are looking for.
So this should be the code:
def addContact(request):
if request.method == 'POST':
form = MyForm(request.POST) # Edit made here, you need to define the form you are using from your forms.py
if form.is_valid():
new_contact = Contact(
full_name = request.POST ['fullname'],
relationship = request.POST ['relationship'],
email = request.POST ['email'],
phone_number = request.POST ['phone-number'],
address = request.POST ['address'],
)
new_contact.save()
return redirect('/contact')
return render(request, 'contact/new.html')
Edit:
It is also good practice to include an else in the request.method if statement.
Another good practice is to use form.cleaned_data.get() for passing your data.
Example:
def getData(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
form.save()
data = form.cleaned_data.get('dataField')
Alternative would be add a required attribute on your HTML inputs but that might not help everytime since the user could just remove it from the Inspect tab and submit an empty form LOL. Just use the form.is_valid or just tryna check if the fields are empty using:
if full_name == null OR relationship == null OR email == null OR phone_number == null OR address == null:
#Some Return an error statement
Hope that helps! (:

How to get ID from another model in django?

I have 2 models that it must be input to database in same time by submit button in my template.
this is my first model
class DataPribadiSiswa(models.Model):
SiswaID = models.AutoField(primary_key=True)
WaliKelasID=models.CharField(max_length=5,blank=True,null=True)
SiswaNIS = models.IntegerField(null=True, blank=True)
second model
class RiwayatSekolah(models.Model):
SekolahID=models.AutoField(primary_key=True)
SiswaID_FK=models.CharField(max_length=10,blank=True,null=True)
and this is my view.py
def tambah_siswa(request):
form = datasiswa(request.POST)
form2 = riwayatsekolah(request.POST)
if request.method == 'POST':
if form.is_valid() and form2.is_valid():
form.save()
form2.save()
return redirect('index')
else:
form = datasiswa()
form2 = riwayatsekolah()
return render(request, 'siswa/tambah_siswa.html', {'form': form, 'form2': form2})
how to insert SiswaID fromDataPribadiSiswa to SiswaID_FK in RiwayatSekolah at same time where all form is blank?
You can use ForeignKey field.
class RiwayatSekolah(models.Model):
SekolahID=models.AutoField(primary_key=True)
SiswaID_FK=models.ForeignKey(DataPribadiSiswa)
Now in view you can try to save Siswa instance first and then to add it into Sekolah instance:
def tambah_siswa(request):
form = datasiswa(request.POST)
form2 = riwayatsekolah(request.POST)
if request.method == 'POST':
if form.is_valid() and form2.is_valid():
siiswa_instance = form.save()
Sekolah_instance = form2.save(commit=False)
Sekolah_instance.SiswaID_FK = siiswa_instance
Sekolah_instance.save()

How do I save m2m field session from a form to another?

I'm doing a multi step form where everything is saved at the end. In my models I have a m2m checkbox field and I'm using django Sessions to grab the forms datas to show it on the final step.
The issue is that the m2m field (checkboxes) is not saved when I submit the final form.
Here is my views file :
views.py
def step1(request):
initial={'name': request.session.get('name', None), 'checkbox': request.session.get('checkbox', (False,))} #cookies
form = FormOneForm(request.POST or None, initial=initial)
if request.method == 'POST':
if form.is_valid():
request.session['name'] = form.cleaned_data['name']
request.session['checkbox'] = form.cleaned_data.get('checkbox')
return HttpResponseRedirect(reverse('step2'))
return render(request, 'step1.html', {'form': form})
def step2(request):
form = FormTwoForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
formtwo = form.save(commit=False)
formone2 = FormOne.objects.create(checkbox=request.session.get('checkbox')) #error is here
formone = FormOne.objects.create(name=request.session['name'])
formtwo.owner = formone
formtwo.save()
formone2.save_m2m()
return HttpResponseRedirect(reverse('step3'))
return render(request, 'step2.html', {'form': form})
models.py
class Font(models.Model):
font_name = models.CharField(max_length=100)
font_family = models.CharField(max_length=100)
font_link = models.CharField(max_length=100)
...
class FormOne(models.Model):
name = models.CharField(max_length=40)
checkbox = models.ManyToManyField(Font, blank=True)
...
class FormTwo(models.Model):
owner = models.ForeignKey(FormOne)
name = models.CharField(max_length=40)
...
this code gives me this error :
'checkbox' is an invalid keyword argument for this function
How can I achieve what I am trying to realise ?
Try to save object first:
formone2 = FormOne.objects.create(name=request.session['name'])
formone2.checkbox.add(request.session.get('checkbox')
The problem is that you need to save object before use Many-to-many relations. See docs:
You can’t associate it with a Publication until it’s been saved

Django form passing key to next form

I want to pass a pk from one form to another so that it can be used as the foreign key for the second form. Here are the model:
models.py
class CompanyDetails(models.Model):
name = models.CharField(max_length=100)
class CompanyDetailsForm(forms.ModelForm):
class Meta:
model = CompanyDetails
class DataRequest(models.Model):
company = models.ForeignKey(CompanyDetails, default="0")
agency_name = models.CharField(max_length=100)
class DataRequestForm(forms.ModelForm):
class Meta:
model = DataRequest
exclude = ['company']
And here is the view for the first form:
views.py
def index(request):
if request.method == 'POST':
form = CompanyDetailsForm(request.POST or None)
if form.is_valid():
data = form.save(commit=False)
data.save()
return HttpResponseRedirect(reverse('canareeform:datarequest', data.id))
else:
form = CompanyDetailsForm()
return render(request, 'canareeform/index.html', {'form': form})
How should I set up my second view so that the form will save an object that has the foreign key for the object created by the first form in it?
I got it to work by passing the primary key of the first object through the url. It goes abc.com/form -> abc.com/form/16/datarequest. Not super ideal since by changing the number in the url the second object will use some other foreign key.
views.py
def index(request):
if request.method == 'POST':
form = CompanyDetailsForm(request.POST or None)
if form.is_valid():
data = form.save(commit=False)
data.save()
return HttpResponseRedirect(reverse('canareeform:datarequest', args=(data.id,)))
else:
form = CompanyDetailsForm()
return render(request, 'canareeform/index.html', {'form': form})
def datarequest(request, company_id):
if request.method == 'POST':
form = DataRequestForm(request.POST or None)
if form.is_valid():
data = form.save(commit=False)
data.company = CompanyDetails.objects.get(pk=company_id)
data.save()
return HttpResponse("Thanks")
else:
form = DataRequestForm()
return render(request, 'canareeform/datarequest.html', {'form': form})
If anyone has a better solution I'd love to hear it.

Add a ModelChoiceSet to an inlineformset after it's generated

I have an inlineformset based off a very simple Model form. I'd like a way to add a choiceset to one of the fields of the form, but I want to do it from the views.py file as the choiceset is generated dynamically. Is this possible? Python2.7, Django1.5
models.py
class RoleForm(ModelForm):
class Meta:
model = Role
fields = ['role_type', 'user', 'item']
views.py
def new(request):
''' Create a new application '''
user = request.user
associatedRoleTypes = getAssociatedRoleTypes(Application)
#^^^this returns a queryset, and is what I want model_choices to be equal to
RoleFormSetFactory = inlineformset_factory(Application, Role, can_delete=False, form=RoleForm)
if request.method == 'POST':
applicationForm = ApplicationForm(request.POST)
if applicationForm.is_valid():
new_application = applicationForm.save()
roleInlineFormSet = RoleFormSetFactory(request.POST, instance = new_application)
if roleInlineFormSet.is_valid():
roleInlineFormSet.save()
else:
print roleInlineFormSet.errors
return redirect(reverse('index'))
else:
applicationForm = ApplicationForm()
roleInlineFormSet = RoleFormSetFactory()
return render(request, 'who/editapp.html',
{'roleInlineFormSet': roleInlineFormSet,
'applicationForm': applicationForm
})

Categories

Resources