Checkboxes True or False not loading into form properly - django

I'm using a ModelForm to load a Form and all of the values are pulling properly except the checkboxes. They always load in unchecked whether or not their state is True or False.
I've tried creating the checkbox fields like:
<label>
<input type="checkbox" checked='{{ form.instance.hot }}'>
<span>Hot{{ form.hot }}</span>
</label>
<li>CSR: {{ form.csr }}</li>
{% if request.user|has_group:"Customer Service Manager" %}
<li>Documents: {{ form.documents }}</li>
{% endif %}
<li>Notes: {{ form.notes }}</li>
<li>First Name:{{ form.first_name }}</li>
<li>Last Name: {{ form.last_name }}</li>
<li>Address: {{ form.address }}</li>
Which results in the checkbox always being checked instead matching what's in the table for that client.
models.Client
class Client(models.Model):
#Account Information
client_status =[("1", "Active"), ("0", "Inactive")]
WHOPAYS =[("0", "Not Applicable"), ("1", "They Pay"), ("2", "We Pay")]
hot = models.BooleanField(default=False, required=False)
region = models.ForeignKey('Region', to_field='region_id', on_delete=None, null=True, blank=True)
sub_account = models.ForeignKey('self', on_delete=models.CASCADE, related_name='sub_account_of_client', null=True, blank=True)
created = models.DateField('Account Creation', auto_now_add=True)
client_class = models.ForeignKey('ClientClass', on_delete=None, null=True, blank=True)
category = models.ForeignKey('ClientCategory', on_delete=None, null=True, blank=True)
they_pay = models.BooleanField('They Pay', default = False, required=False)
we_pay = models.BooleanField('We Pay', default = False, required=False)
csr = models.ForeignKey(settings.AUTH_USER_MODEL, to_field='extension', on_delete=None, blank=True, null=True, limit_choices_to= Q( groups__name = 'Customer Service'))
#Upload location /<ClientID>/documents/*
documents = models.FileField(null=True, blank=True, upload_to=client_directory_path)
notes = models.TextField(null=True, blank=True)
active_status = models.CharField('Status', max_length=1, choices=client_status, default="1")
client = models.CharField('Client/Company Name',max_length=200, null=True, blank=True)
account_number = models.CharField(max_length=50, null=True, blank=True)
#Contact Information
first_name = models.CharField('First Name', max_length = 200, null=True, blank=True)
last_name = models.CharField('Last Name', max_length = 200, null=True, blank=True)
email = models.EmailField(max_length = 254, null=True, blank=True)
main_phone = models.CharField(null=True, blank=True, max_length=50)
alt_phone = models.CharField(null=True, blank=True, max_length=50)
fax = PhoneField(null=True, blank=True)
follow_up = models.DateField('Date to Follow Up', default=three_days())
#Billing and Shipping Addresses
address = models.CharField('Address Line 1', max_length=200, null=True, blank=True)
address2 = models.CharField('Address Line 2', max_length=200, null=True, blank=True)
city = models.CharField('City', max_length=200, null=True, blank=True)
zipcode = models.CharField('Zip Code', max_length=200, null=True, blank=True)
state = models.CharField('State', max_length=2, null=True, blank=True, choices=STATE_CHOICES)
country = models.CharField('Country', max_length=200, null=True, blank=True, default='USA')
def __str__(self):
if self.sub_account:
return '{0}: Sub-Account for {1}'.format(self.client, self.sub_account)
else:
return '{0} - {1}'.format(self.client, self.zipcode)
views.client_view
def client_view(request):
FormSet = modelformset_factory(Client, form=ClientSheet, extra=0)
if request.method == 'POST':
formset = FormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save(commit=False)
formset.save()
return HttpResponseRedirect('/client/')
else:
query = Client.objects.filter(follow_up__lte=date.today(), csr__extension=request.user.extension).order_by('-hot')
currentcall = Client.objects.filter(follow_up__lte=date.today(), csr__extension=request.user.extension).order_by('-hot').first()
paginator = Paginator(query, 1) # Show 1 forms per page
page = request.GET.get('page')
try:
objects = paginator.page(page)
except PageNotAnInteger:
objects = paginator.page(1)
except EmptyPage:
objects = paginator.page(paginator.num_pages)
page_query = Client.objects.filter(id__in=[object.id for object in objects])
formset = FormSet(queryset=page_query)
context = {'objects': objects, 'formset': formset, 'callqueue': query, 'currentcall': currentcall}
return render(request, 'main/client.html', context)

Whenever Checkbox will always be checked as soon as you pass checked attribute.
Your template should look like
<input type="checkbox" {{ form.instance.hot|yesno:"checked,'',''" }}>

Related

Django How to get the value from many to many relationship

I have a product update form where I want to update the sizes of a product, with a many-to-many column relation.
I am able to get the saved values but I can't get item ID or size.
{{item.id}} shows empty value.
{% for item in form.size.value %}
<span>
<input type="checkbox" name="size" id="{{ item.id }}" value="{{ item.id }}">
<label for="{{ item.id }}">{{ item }}</label>
</span>
{% endfor %}
SIZE MODEL
class Size(models.Model):
identifier = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
size = models.CharField(max_length=200, unique=True, null=False, blank=False)
active = models.BooleanField(default=True)
date_created = models.TimeField(verbose_name='Date Created', auto_now_add=True)
date_updated = models.DateTimeField(verbose_name='Last Updated', auto_now=True)
PRODUCT MODEL
class Product(models.Model):
identifier = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
name = models.CharField(max_length=200, null=False, blank=False)
slug = models.SlugField(max_length=30, unique=True)
showcased = models.BooleanField(default=False)
recommended = models.BooleanField(default=False)
active = models.BooleanField(default=True)
price = models.DecimalField(max_digits = 5, decimal_places = 2, null=True, blank=True)
pagetitle = models.CharField(max_length=200, null=True, blank=True)
shortDescription = models.CharField(max_length=200, null=True, blank=True)
longDescription = HTMLField(null=True, blank=True)
specifications = models.TextField(null=True, blank=True)
features = models.TextField(null=True, blank=True)
care = models.TextField(null=True, blank=True)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
subCategory = models.ForeignKey(SubCategory, null=True, blank=True, on_delete=models.PROTECT)
image = models.ManyToManyField(Image, blank=True)
size = models.ManyToManyField(Size, blank=True)
tag = models.ManyToManyField(Tag, blank=True)
date_created = models.TimeField(verbose_name='Date Created', auto_now_add=True)
date_updated = models.DateTimeField(verbose_name='Last Updated', auto_now=True)
I have tried different ways but not had success in getting the name or id
def updateProduct(request, pk):
categories = Category.objects.all()
sizes = Size.objects.all()
tags = Tag.objects.all()
product = Product.objects.get(id=pk)
subCategories = SubCategory.objects.filter(category=product.category.id)
images = product.image.all()
if request.method == 'POST':
form = ProductForm(request.POST, instance=product)
if form.is_valid():
form.save()
return redirect('product-panel')
form = ProductForm(instance=product)
return render(request, 'public/views/backend/product-update.html', {'categories':categories,'subCategories':subCategories, 'sizes':sizes, 'tags':tags, 'form':form, 'images':images})

Django parent.child_set no errors but not showing in html

Views.py defining the context in view
def customers(request, pk):
customer = Customer.objects.get(id=pk)
issues = customer.issue_set.all()
receives = customer.receive_set.all()
context={'customer':customer,'issues':issues,'receives':receives}
return render(request,'accounts/customers.html')
in html
<div class="col-md">
<div class="card card-body">
<h5>Contact Information</h5>
<hr>
<p>Email: {{customer.email}}</p>
<p>Phone: {{customer.phone}}</p>
</div>
</div>
{% for issue in issues %}
<td>{{issue.item}}</td>>
<td>{{issue.item.category}}</td>
<td>{{issue.date_created}}</td>
<td>{{issue.status}}</td>
<td><a href="">UPDATE</td>
<td><a href="">DELETE</td>
{% endfor %}
#Model
class Item(models.Model):
CATEGORY = (
('Gudang Kering', 'Gudang Kering'),
('Gudang Basah','Gudang Basah'),
)
name = models.CharField(max_length=200,null= True)
stock = models.IntegerField(default='0', blank=False, null=True)
category = models.CharField(max_length=200,null= True,choices=CATEGORY)
reorderlevel = models.IntegerField(default='0', blank=False, null=True)
maxreorderlevel = models.IntegerField(default='0', blank=False, null=True)
description = models.CharField(max_length=200,null= True, blank= True)
date_created = models.DateTimeField(auto_now_add= True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.name
class Issue(models.Model):
STATUS = (
('Pending', 'Pending'),
('Granted','Granted'),
('Denied','Denied'),
)
customer = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
item = models.ForeignKey(Item, null=True, on_delete= models.SET_NULL)
quantity = models.IntegerField(default='0', blank=False, null=True)
date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
status = models.CharField(max_length=200,null= True, choices=STATUS)
def __str__(self):
return self.status + ' ' +str(self.customer)
I tried to get the object by id of the customer in order to make it a dynamic url where the url will depend on str:pk of customer id
i managed to show output data if i do
customer = Customer.objects.all #but that will show all the customer
so i tried as in the view to get the id
and define it with parent.child_set.all
but it doesn't show up,even the text update and delete don't show up in

Select a model instance from HTML form on Django

I am trying to get my html form to allow me to pass the company model instance. As of now, I can pull the names of each company instance, however, what would I put into the value attibute of the option field to have it select the instance correctly?
<option value="what to put here?">{{Company.name}}</option>
I was hoping to do this through HTML forms and not Django forms as I have used AJAX to make a nice little live-updating interface.
models.py
class Company(models.Model):
name = models.CharField(max_length=30, null=True, blank=True)
email = models.CharField(max_length=40, null=True, blank=True)
phone = models.CharField(max_length=15, null=True, blank=True)
address = models.CharField(max_length=100, null=True, blank=True)
notes = models.CharField(max_length=400, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True, blank=True)
updated = models.DateTimeField(auto_now=True, blank=True)
class Meta:
ordering = ["name"]
def __str__(self):
return self.name
class Contact(models.Model):
firstname = models.CharField(max_length=20, null=True, blank=True)
lastname = models.CharField(max_length=20, null=True, blank=True)
email = models.CharField(max_length=40, null=True, blank=True)
phone = models.CharField(max_length=15, null=True, blank=True)
title = models.CharField(max_length=20, null=True, blank=True)
notes = models.CharField(max_length=400, null=True, blank=True)
company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["lastname"]
def __str__(self):
return self.firstname
views.py
class contact_manager(ListView):
template_name = 'crm/contact_manager.html'
context_object_name = 'contact_manager'
queryset = Contact.objects.all()
def get_context_data(self, **kwargs):
context = super(contact_manager, self).get_context_data(**kwargs)
context['contact'] = Contact.objects.all()
context['company_list'] = Company.objects.all()
# And so on for more models
return context
contact_manager.html
<div class="form-group">
<select class="form-control" name="company" placeholder="Company">
<option value="">Please select a company</option>
{% for Company in company_list %}
<option value="{{Company.name}}">{{Company.name}}</option>
{% endfor %}
</select>
</div>
If you are looking for a unqiue identifier for each option, that links to a model instance on the backend, that is what the ID field is for (Company.id):
<option value="{{ Company.id }}">{{Company.name}}</option>
Then on the backend you can retrieve the model with the posted id:
Company.get(id=posted_id)
Note: id is by default added to your model as the primary key and is a auto incrememting integer.

Django odd boolean bahavior

I have a boolean field for whether or not an item is active:
is_active = models.BooleanField(default=True)
It seems pretty straightforward, my template will display items that are active:
{% for p in products|dictsortreversed:"id" %}
{% if p.is_active %}
<a href="{{ p.get_absolute_url }}">
{{ p.name }}
</a>
{% endif %}
For some reason all items are returned even if the field is 0 in the database. When I uncheck the boolean field in the django admin, it updates correctly to 0 in the database, but still shows as being checked in the admin...
It seems like django is reading the field as True, regardless of the boolean value.
Model
class Product(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name')
price = models.DecimalField(max_digits=9, decimal_places=2, blank=True, default=0.00)
old_price = models.DecimalField(max_digits=9, decimal_places=2, blank=True, default=0.00)
image = models.CharField(max_length=50)
is_active = models.BooleanField(default=True)
quantity = models.IntegerField()
description = models.TextField()
meta_keywords = models.CharField('Meta Keywords', max_length=255, help_text='Comma-delimited set of SEO keywords for meta tag')
meta_description = models.CharField('Meta Description', max_length=255, help_text='Content for description meta tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
categories = models.ForeignKey(Category, null=True)
publish_date = models.DateField(blank=True, null=True)
issue_one = models.CharField(blank=True, null=True, max_length=255)
issue_two = models.CharField(blank=True, null=True, max_length=255)
issue_three = models.CharField(blank=True, null=True, max_length=255)
class Meta:
db_table = 'products'
ordering = ['-created_at']
def __unicode__(self):
return self.name
#models.permalink
def get_absolute_url(self):
return ('catalog_product', (), {'product_slug': self.slug})
View:
def index(request, template_name="catalog/index.html"):
""" site home page """
page_title = 'Visible Language Ordering'
return render_to_response(template_name, locals(), context_instance=RequestContext(request))

dynamic formset doesn't handle file upload data properly

I'm building a dynamic formset based on the dynamic-formset jquery plug-in: I've constructed an alpha model that works except that it doesn't commit data from any of the fileupload fields to the database. Have I blown my formset view function?
#forms.py
class PostEntryForm(ModelForm):
class Meta:
model = PostEntry
ContactFormset = formsets.formset_factory(PostEntryForm)
#models.py
class PostEntry(models.Model):
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
job_number = models.CharField(validators=[RegexValidator(regex='^\w{8}$', message='Please enter a valid job number', code='nomatch')], max_length=8, unique=False, blank=False, null=False)
cell_number = models.CharField(max_length=4, unique=False, blank=True, null=True)
post_title = models.CharField(max_length=64, unique=False, blank=True, null=True)
date = models.DateField(("Date"), default=datetime.date.today)
post_type = models.CharField(max_length=64, choices=POST_CHOICES)
post_round = models.CharField(max_length=20, blank=False, null=False)
docfile = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
url_link = models.URLField(blank=True, null=False, max_length=300)
misc_link = models.CharField(max_length=64, blank=True, null=True)
link_misc = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
misc_link2 = models.CharField(max_length=64, blank=True, null=True)
link_misc2 = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
mobile_view_url = models.URLField(validators=[RegexValidator(regex='^(http|https)://', message='url must begin with http or https', code='nomatch')], blank=True, null=False, max_length=300)
link_pdf = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
link_html = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
link_report = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
link_text = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
link_zip = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
def __unicode__ (self):
return u'%s %s %s %s %s' % (self.client, self.job_number, '-', self.cell_number, self.post_title)
def save(self, force_insert=False, force_update=False):
self.job_number = self.job_number.upper()
self.cell_number = self.cell_number.upper()
super(PostEntry, self).save(force_insert, force_update)
class Meta:
ordering = ['-date', 'cell_number']
class Admin:
pass
#views.py
def formset(request, formset_class, template):
if request.method == 'POST':
formset = formset_class(request.POST, request.FILES)
if formset.is_valid():
for form in formset.forms:
form.save()
return HttpResponseRedirect('/main')
else:
formset = formset_class()
return render_to_response(template, {'formset': formset},
context_instance=RequestContext(request))
If it's not sending just your fileupload, I guess it could be a missing enctype attribute on your form.
So, try to do this:
<form action="" method="POST" enctype="multipart/form-data">
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
</form>