How do I implement an auto-calculated field in Django using JavaScript? - javascript

I would like to auto-calculate my deposit amount height in a form. Therefore, I included JavaScript to make it happen.
The idea: After entering the net into the form, the suitable deposit height will be displayed in the amount field. And there is nothing to do for the user here.
I really appreciate any suggestions since I am total beginner with Django.
I tried to access the field via known methods like netObj.fishing_type and ['fishing_type'] but I feel like this is not the issue. Somehow I can't access the model fields of the triggered record.
html file:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content%}
<p> </p>
<div class ="form-group">
<form method="POST" action="{% url 'put_deposit' %}" enctype="multipart/form-data">
{% csrf_token %}
{{form|crispy}}
<script>
// Get the input elements by targeting their id:
const net_input = document.getElementById('id_net');
const vessel_input = document.getElementById('id_vessel');
const amount = document.getElementById('id_amount');
// Create variables for what the user inputs, and the output:
let netObj = 0;
let vesselObj = 0;
let height = 0;
// Add an event listener to 'listen' to what the user types into the inputs:
net_input.addEventListener('input', e => {
netObj = e.target.value;
console.log(netObj);
updateAmount()
});
// Update the value of net based on what the user inputs in for gross and tier
function updateAmount(e) {
if ((Date.now() - netObj.prod_date)>3) {
height = 900;
} else {
height = 600;
}
term = netObj['fishing_type];
if(term == 'Surrounding' || term == 'Trawl'){
height = height *2;
} else if(term == 'Longline' || term=='Seine' ){
height = height *1.5;
} else{
height = height *1.25;
}
amount.value = height;
}
</script>
<button type="Submit" class="btn btn-outline-dark">Submit</button></div>
{% endblock content%}
Extract of models.py:
class Net(models.Model):
net_id = models.IntegerField(primary_key=True, default = None)
prod_date = models.DateField()
weight = models.DecimalField(default=0, max_digits=6, decimal_places=2)
material = models.CharField(max_length=100)
fishing_type = models.CharField(max_length=100, default=None)
def __str__(self):
return '{}: {}'.format(self.fishing_type, self.material)
class Deposit(models.Model):
dep_id = models.BigAutoField(primary_key=True, default=None)
dep_date = models.DateField()
harbour = models.CharField(max_length=100)
vessel = models.ForeignKey(Vessel, to_field='VID', on_delete=models.CASCADE)
net = models.ForeignKey(Net, to_field='net_id', on_delete=models.CASCADE)
amount = models.DecimalField(default=0, max_digits=8, decimal_places=2)
forms.py:
from django.forms import ModelForm
from myapp.models import *
class UploadForm(ModelForm):
class Meta:
model = Deposit
fields = ['dep_date', 'harbour',
'vessel', 'net',
'amount']

Related

how can i create a table entry in django views

I want to make a system of likes on the site, and I need to create an entry in the table on the button. When a person clicks on like, the table writes row 1 and when dislike 0
views.py
def forum(requset):
model = Quetions.objects.all()
answer = Answer.objects.all()
count = Answer.objects.all().count()
count_answer = Quetions.objects.all().count()
paginator = Paginator(model, 1) # Show 25 contacts per page.
page_number = requset.GET.get('page')
question_id = requset.GET.get('id',False)
page_obj = paginator.get_page(page_number)
requestanswer = requset.GET.get('id',False)
like_disli = like.objects.filter(like_dislike = "1").count()
dislike = like.objects.filter(like_dislike = "0").count()
createlike =
objects = {
'model': page_obj,
'answer':answer,
'count':count,
'count_question':count_answer,
'page_obj':page_obj,
'question':question_id,
'id':model,
'request':requestanswer,
'like':like_disli,
'dislike':dislike,
'createlike':createlike,
}
return render(requset,'forum.html',objects)
forum.html
<span>
<i class="fas fa-thumbs-up" style="color: blue;margin-right: 5px;" onclick="incrementClick()"></i>{{like}}<i class="fas fa-thumbs-down" style="color: red;margin-right: 5px;margin-left: 10px;" onclick="dislikeclick()"></i>{{dislike}}
</span>
{% block js %}
<script>
var a = "{{createlike}}"
function incrementClick() {
a
}
function dislikeclick() {
dislikedisplay(++dislikecounter);
}
function updateDisplay(val) {
document.getElementById("counter-label").innerHTML = val;
}
function dislikedisplay(val){
document.getElementById("counter").innerHTML = val
}
</script>
{% endblock js %}
tell me how to do it???

How to fetch image from django FileResponse using javascript

I would like to render an image in html using javascript, and the image is being retrieved from a django view using the fetch api. I think I am close but can't figure out how to display image in browser using javascript.
view #1: (url: builds/imgretrieve/int:device/)
class imgRetrieve(generics.GenericAPIView):
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, *args, **kwargs):
device = self.kwargs['device']
deviceVals = CustomUser.objects.get(id=device)
buildImg = ImageRequest.objects.filter(author = deviceVals).latest("dateAdded")
return FileResponse(buildImg.image)
When I access this view directly using a browser, it displays an image as expected.
However when I want the javascript to display it in a different view, it just displays [object Response] instead of the image.
Here is how I am attempting to do this:
view #2 (url: builds/chamberCam/int:device/)
class chamberCamView(LoginRequiredMixin,TemplateView):
template_name = 'chamberCam.html'
login_url = 'login'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
device = self.kwargs['device']
deviceVals = CustomUser.objects.get(id=device)
CustomUser.objects.filter(id = device).update(imgRequested = True, imgReceived = False,)
context['deviceName'] = deviceVals.UserDefinedEquipName
context['deviceID'] = deviceVals.id
return context
html
{% block body %}
<p hidden id = "id">{{ deviceID }}</p>
<div class="">Chamber cam for {{deviceName}}</div>
<div class="imageDiv" id = "image">
</div>
{% load static %}
<script src="{% static 'camera/chamberCam.js' %}"></script>
{% endblock %}
chamberCam.js
let deviceID = document.getElementById("id").innerHTML;
getImg(deviceID)
function getImg(deviceID){
fetch('http://127.0.0.1:8000/builds/imgretrieve/' + deviceID + '/')
.then(function(res){
return res;
})
.then(function(data) {
let image = document.getElementById("image");
console.log(data)
image.innerHTML = `
<img>${data}</img>
`;
})
.catch(function(err){
console.log(err);
});
}
I was over complicating it..
I replaced the javascript code with:
let deviceID = document.getElementById("id").innerHTML;
addimage(deviceID)
function addimage() {
var img = new Image();
img.src = 'http://127.0.0.1:8000/builds/imgretrieve/' + deviceID + '/'
image.appendChild(img);
}

Javascript array to Django many to many field

I have a Django form with Area and Subarea. The subarea input is a ManytoManyField (not every area is a subarea but all subarea is an area), but I have to style it in the frontend, in a way that I can add and remove elements from a text input before I submit the form. I have already implemented a JavaScript function that collects all subareas in an array.
I would like to know how can I pass this array to Django, so it can go to the Subarea field and save it. Is there any possible way?
Django models.py:
class Area(models.Model):
name = models.CharField(max_length=50)
is_subarea = models.BooleanField(default=False)
Subarea = models.ManyToManyField('Area', blank=True)
def __str__(self):
return self.name
Django views.py:
class AddLanguageView(LoginRequiredMixin, CreateView):
model = Area
template_name = 'language/add-language.html'
form_class = LanguageForm
def post(self, request, *args, **kwargs):
area = Area()
area.name = request.POST.get('nome')
area.subarea = request.POST.get('Subarea')
area.save()
return render(request, 'language/add-language.html')
let sublanguageArray = [];
let i = 0;
const addSublanguage = () => {
// Add to array
const sublanguage = document.getElementById("sublanguage-input").value;
sublanguageArray.push({
name: sublanguage,
id: i
});
// Screen Preview
document.querySelector('#sublanguages').innerHTML += `
<div>
<div class="delete-sublanguage">
<img src="/static/assets/delete.png" id-delete="${i}" onclick="deleteSublanguage(this)">
</div>
${sublanguage}
</div>
`;
i++
}
// Delete html preview and from array
const deleteSublanguage = (item) => {
const block = item.parentElement.parentElement;
block.parentNode.removeChild(block);
sublanguageArray = sublanguageArray.filter(sub => sub.id !== +item.getAttribute('id-delete'));
};
<form method="POST" class="add-language-form" >
{% csrf_token %}
<div class="language-title-box" >
<h2>Linguagem:</h2>
{{ form.name }}
</div>
<div class="subarea-box">
<h2>Sub-linguagem</h2>
<input type="text" id="sublanguage-input">
<button id="submit-sublanguage" onclick="addSublanguage()">ADD</button>
<div id="sublanguages"></div>
</div>
<div class="submit-lang-box" >
<input type="submit" value="ADD AREA" id="addLanguageButton" next="partials/base.html">
</div>
</form>

Django ValidationError after javascript modifies the choice field

I'm working on a job scheduling app for a REST API and I've created a Tasks, Nodes, URL, Adom, BaseOptions, and Jobs tables in Django. The BaseOptions have a foreign key for src_adom and dst_adom and the Jobs have foreign keys for the Task, src_node, dst_node, url and baseOptions. I didn't use a ModelForm but a generic Form instead and just loaded up the initial values and choices for all the fields I need in order to process a job. I did that because I couldn't figure out how to make the form change when the user selects a different node and want to change the select field for the corresponding adoms. I'm using javascript to detect a new node and changing the select field with the appropriate adoms for that node. I get a validation error if I change the initial source and/or destination node and modify the adom selection list. I know it is because my list of choices have changed from what I initially rendered the form with.
Is there a way to change the choices associated with a ChoiceField during the "clean" method? I can't seem to find that in the documents or digging through the Django code on github.
Models:
FMG = 'FMG'
FOS = 'FOS'
NODE_TYPE_CHOICES = (
(FMG, 'FMG'),
(FOS, 'FOS'),
)
# Create the types of tasks to run
class TaskType(models.Model):
name = models.CharField(max_length=200)
command = models.CharField(max_length=200)
node_type = models.CharField(max_length=60, choices=NODE_TYPE_CHOICES, default=FMG)
def __str__(self):
return self.name
# Create the nodes for tasks to be run on
class Node(models.Model):
ip = models.GenericIPAddressField(protocol='IPv4')
name = models.CharField(max_length=200)
apiname = models.CharField(max_length=200)
apikey = EncryptedCharField(max_length=200)
node_type = models.CharField(max_length=60, choices=NODE_TYPE_CHOICES, default=FMG)
def __str__(self):
return "{0} ({1})".format(self.name, self.ip)
class FMG_URLManger(models.Manager):
def get_queryset(self):
return super(FMG_URLManger, self).get_queryset().filter(node_type='FMG')
class FOS_URLManger(models.Manager):
def get_queryset(self):
return super(FOS_URLManger, self).get_queryset().filter(node_type='FOS')
class NodeURL(models.Model):
name = models.CharField(max_length=200)
node_type = models.CharField(max_length=60, choices=NODE_TYPE_CHOICES, default=FMG)
table_id = models.CharField(max_length=100)
table_url = models.CharField(max_length=200)
help_text = models.CharField(max_length=400, null=True)
filename = models.CharField(max_length=200)
objects = models.Manager()
fmg_objects = FMG_URLManger()
fos_objects = FOS_URLManger()
def __str__(self):
return self.name
class Adom(models.Model):
name = models.CharField(max_length=200)
version = models.CharField(max_length=60)
fmg = models.ForeignKey(Node, related_name='fmgs', on_delete=models.CASCADE)
class Meta:
unique_together = (("name", "version", "fmg"),)
def __str__(self):
return self.name
class BaseOption(models.Model):
src_adom = models.ForeignKey(Adom, related_name="src_adom", on_delete=models.CASCADE)
dst_adom = models.ForeignKey(Adom, related_name="dst_adom", on_delete=models.CASCADE, null=True)
name_filter = models.CharField(max_length=100, null=True)
site_name_filter = models.CharField(max_length=100, null=True)
policy_id_list_filter = models.CharField(max_length=60, null=True)
keep_policy_id = models.BooleanField(default=False)
disable_policy = models.BooleanField(default=False)
import_only = models.BooleanField(default=False)
def __str__(self):
return self.src_adom.name
# Create the actual request for jobs
class Job(models.Model):
UNKNOWN = 'Unknown'
PENDING = 'Pending'
SUCCESS = 'Success'
FAILURE = 'Failure'
STATUS_CHOICES = (
(UNKNOWN, 'Unknown'),
(PENDING, 'Pending'),
(SUCCESS, 'Success'),
(FAILURE, 'Failure'),
)
description = models.CharField(max_length=400)
task = models.ForeignKey(TaskType, related_name='job_task', on_delete=models.CASCADE)
src_node = models.ForeignKey(Node, related_name='src_node', on_delete=models.CASCADE)
urls = models.ManyToManyField(NodeURL)
dst_node = models.ForeignKey(Node, related_name='dst_node', on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='jobs', on_delete=models.CASCADE)
opts = models.OneToOneField(BaseOption, related_name='job', on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
run_at = models.DateTimeField(editable=True, default=timezone.now)
start = models.DateTimeField(editable=True, null=True)
finished = models.DateTimeField(editable=True, null=True)
status = models.CharField(max_length=12, choices=STATUS_CHOICES, default=UNKNOWN, editable=True)
status_text = models.TextField(editable=True, null=True)
file_location = models.CharField(max_length=4086, editable=True, null=True)
objects = models.Manager()
def __str__(self):
return self.description
The ScheduleFormView:
class ScheduleView(LoginRequiredMixin, generic.FormView):
login_url = '/login/'
redirect_field_name = 'redirect_to'
template_name = 'tools/schedule.html'
form_class = ScheduleJobForm
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
print("Got a POST request, checking for valid...")
if form.is_valid():
# <process form cleaned data>
taskID_nodeType = form.cleaned_data['task']
description = form.cleaned_data['description']
src_nodeID_name = form.cleaned_data['src_node']
dst_nodeID_name = form.cleaned_data['dst_node']
urlsID_tableID = form.cleaned_data['urls']
src_adomID = form.cleaned_data['src_adom']
dst_adomID = form.cleaned_data['dst_adom']
name_filter = form.cleaned_data['name_filter']
site_name_fiter = form.cleaned_data['site_name_fiter']
policy_id_list_filter = form.cleaned_data['policy_id_list_filter']
taskID = taskID_nodeType.split('.')[0]
src_nodeID = src_nodeID_name.split('.')[0]
dst_nodeID = dst_nodeID_name.split('.')[0]
# urlsID_tableID was a list, now it's a single URL
# urlIDs = []
# for urlID_tableID in urlsID_tableID:
# urlID = urlID_tableID.split('.')[0]
# print("Got back id {}".format(urlID))
# urlIDs.append(urlID)
urlID = urlsID_tableID.split('.')[0]
urls = NodeURL.objects.filter(id=urlID)
# print("List of URLS = {}".format(urls))
src_node = Node.objects.get(id=src_nodeID)
dst_node = Node.objects.get(id=dst_nodeID)
task = TaskType.objects.get(id=taskID)
src_adom = Adom.objects.get(id=src_adomID)
dst_adom = Adom.objects.get(id=dst_adomID)
job = Job(description=description,
task=task,
src_node=src_node,
dst_node=dst_node,
user=request.user)
opts = BaseOption(src_adom=src_adom,
dst_adom=dst_adom,
name_filter=name_filter,
site_name_filter=site_name_fiter,
policy_id_list_filter=policy_id_list_filter)
opts.save()
job.opts = opts
job.save()
for url in urls:
url.job_set.add(job)
return HttpResponseRedirect('job-list.html')
else:
print("The form was not valid. Return for more processing.")
return render(request, self.template_name, {'form': form})
The ScheduleJobForm (with my unfinished code for 'clean'):
class ScheduleJobForm(forms.Form):
def clean(self):
print("Don't actually check for on src_adom.")
cleaned_data = super(ScheduleJobForm, self).clean()
print(cleaned_data)
adom = self.cleaned_data.get("src_adom")
print(adom)
# if adom < 1:
# raise forms.ValidationError("You didn't choose a valid ADOM.")
print("Don't actually check for on dst_adom.")
cleaned_data = super(ScheduleJobForm, self).clean()
adom = self.cleaned_data.get("dst_adom")
print(adom)
# if adom < 1:
# raise forms.ValidationError("You didn't choose a valid ADOM.")
initTasks = []
selectedNodeType = ''
# Get the first node_type because it will be the default selection of tasks.
for task in TaskType.objects.all().filter(node_type='FMG').order_by('id'):
if selectedNodeType == '':
selectedNodeType = task.node_type
taskChoice = str(task.id) + '.' + task.node_type, task.name
initTasks.append(taskChoice)
# Fill in the initial Nodes for the selected task above.
initNodes = []
selectedNodeID = 0
for node in Node.objects.all().filter(node_type=selectedNodeType).order_by('id'):
nodeChoice = str(node.id) + '.' + node.name, node.name + ' (' + node.ip + ')'
if selectedNodeID == 0:
selectedNodeID = node.id
initNodes.append(nodeChoice)
# Also grab the URL's for those node types and fill them in.
initURLs = []
for url in NodeURL.objects.all().filter(node_type=selectedNodeType).order_by('table_id'):
urlChoice = str(url.id) + '.' + url.table_id, url.name
initURLs.append(urlChoice)
# Since we've got the first node selected, then all the ADOMs are the same. Get them.
initAdoms = []
for adom in Adom.objects.all().filter(fmg_id=selectedNodeID).order_by('id'):
adomChoice = adom.id, adom.name
initAdoms.append(adomChoice)
# Add some hidden fields for the jQuery script to examine.
selected_node_type = forms.CharField(initial=selectedNodeType, widget=forms.HiddenInput)
# After this, a jQuery will have to keep the select fields updated if they are changed.
task = forms.ChoiceField(choices=initTasks)
description = forms.CharField()
src_node = forms.ChoiceField(choices=initNodes)
dst_node = forms.ChoiceField(choices=initNodes)
urls = forms.ChoiceField(choices=initURLs)
# These fields will have to be updated by the jQuery to get the available ADOMS for the selected
# Nodes. We also have to create a custom validator since the choices will change via the JS.
src_adom = forms.ChoiceField(choices=initAdoms)
dst_adom = forms.ChoiceField(choices=initAdoms)
name_filter = forms.CharField()
site_name_fiter = forms.CharField()
policy_id_list_filter = forms.CharField()
The schedule.js script that modifies the adoms:
/*
Of cource, I could use the following but I think the code below is
a bit more readable.
$(function(){
initPage();
}
); */
const coreapi = window.coreapi
const schema = window.schema
$(document).ready(function(){
initPage();
}
);
function initPage() {
console.log("Adding the change code.")
// Fill in the initial srcAdom
getSrcAdom()
// and the initial dstAdom
getDstAdom()
// Hide the wait icon
$('.loading').hide();
$('#id_task').change(
function() {
var currentNodeType = $('#id_selected_node_type').val();
var selectedOption = $('#id_task option:selected').val();
var selectedNodeType = selectedOption.split(".").pop();
if (currentNodeType != selectedNodeType) {
// The more I look at the options, I think this will have to be locked
// into FMG only nodes and create another form for just FOS nodes.
}
}
);
$('#id_src_node').change(
function() {
// Need to change the src adoms
$('.loading').show();
getSrcAdom()
$('.loading').hide();
}
);
$('#id_dst_node').change(
function() {
// Need to change the src adoms
$('.loading').show();
getDstAdom()
$('.loading').hide();
}
);
}
function getSrcAdom() {
var selectedOption = $('#id_src_node option:selected').val();
var selectedNodeName = selectedOption.split(".").pop();
// Clear the src adom options.
$('#id_src_adom')
.find('option')
.remove();
// Initialize a client
var client = new coreapi.Client()
// Interact with the API endpoint
var action = ["adom", "list"]
var params = {
search: selectedNodeName,
}
client.action(schema, action, params).then(function(result) {
// Return value is in 'result'
$.each(result, function(index, obj) {
$('#id_src_adom')
.append('<option value="' + obj["id"] + '">' + obj["name"] + '</option>');
console.log(index + " got '" + obj["name"] + "' (id: " + obj["id"] + ")");
})
})
}
function getDstAdom() {
var selectedOption = $('#id_dst_node option:selected').val();
var selectedNodeName = selectedOption.split(".").pop();
// Clear the src adom options.
$('#id_dst_adom')
.find('option')
.remove();
// Initialize a client
var client = new coreapi.Client()
// Interact with the API endpoint
var action = ["adom", "list"]
var params = {
search: selectedNodeName,
}
client.action(schema, action, params).then(function(result) {
// Return value is in 'result'
$.each(result, function(index, obj) {
$('#id_dst_adom')
.append('<option value="' + obj["id"] + '">' + obj["name"] + '</option>');
console.log(index + " got '" + obj["name"] + "' (id: " + obj["id"] + ")");
})
})
}
function createNodeSelect(data) {
// This was to clear all selected nodes and list new node types.
// It's not going to be used in this form at this time.
// Need to determine data format from JSON return.
// Leaving for future code example of how to clear options
// and replace it with a single option.
$('#id_src_node')
.find('option')
.remove()
.end()
.append('<option value="whatever">text</option>')
.val('whatever');
}
The schedule.html template (with Bootstrap 4 tags):
{% extends "tools/base_site.html" %}
{% block extra_js %}
{% load static %}
<script src="{% static 'rest_framework/js/coreapi-0.1.1.js' %}"></script>
<script src="{% url 'api-docs:schema-js' %}"></script>
<script src="{% static 'js/schedule.js' %}"></script>
{% endblock %}
{% block content %}
<div id="content-main">
<br>
{% block loading %}
{% load static %}
<div id="loading" class="loading">
<img src="{% static 'images/spinning-wait-icons/wait30.gif' %}" alt="Wait" />
<!-- <h3>Loading Data from Server</h3> -->
</div>
{% endblock %}
{% load widget_tweaks %}
{% if form.errors %}
<div class="alert alert-primary">
<button type="button" class="close" data-dismiss="alert">×</button>
{% for field in form %}
{% if field.errors %}
<li>{{ field.label }}: {{ field.errors|striptags }}</li>
{% endif %}
{% endfor %}
</div>
{% endif %}
<form method="post">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% render_field field class="form-control" %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Schedule</button>
</form>
</div>
{% endblock %}
I must confess I didn't go through all the code (it's quite a lot), but at a first glance it looks to me as you should move the initialization of your choices (intTasks, initNodes...) into the __init__ method of your FormClass (ScheduleJobForm).
Because you want the code to be run when you instantiate the class, not when you load the module, right?
So:
class ScheduleJobForm(forms.Form):
selected_node_type = forms.CharField(initial=selectedNodeType, widget=forms.HiddenInput)
task = forms.ChoiceField(choices=initTasks)
# further fields
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# initialize initNodes and the rest
self.fields["src_node"]=initNodes
# and resp.
The form does not care about Javascript, as it is instantiated in the post request after all the Javascript magic on the frontend.
So there doesn't have to (and cannot) be any interaction between Javascript and your form.
I was able to figure out how to dynamically change the choices by using the getter and setter property of ChoiceField (see github repo).
The change I made was in the ScheduleJobForm view. I added these additional lines before calling 'is_valid':
print("Got a POST request, checking for valid...")
print("Fix the choices in the ADOMs first.")
src_nodeID_name = form['src_node'].value()
dst_nodeID_name = form['dst_node'].value()
src_nodeID = src_nodeID_name.split('.')[0]
dst_nodeID = dst_nodeID_name.split('.')[0]
srcAdoms = []
for adom in Adom.objects.all().filter(fmg_id=src_nodeID).order_by('id'):
adomChoice = adom.id, adom.name
srcAdoms.append(adomChoice)
dstAdoms = []
for adom in Adom.objects.all().filter(fmg_id=dst_nodeID).order_by('id'):
adomChoice = adom.id, adom.name
dstAdoms.append(adomChoice)
form.fields['src_adom'].choices = srcAdoms
form.fields['dst_adom'].choices = dstAdoms
For efficiency, I should probably check changed_data and see if the ADOM's have actually changed or not.

Error to create formset factory using django-dynamic-formset

I'm trying to use two formset_factory and django dynamic formsets in the same view, but I'm getting this error
[DOM] Found 2 elements with non-unique id #id_form-TOTAL_FORMS:
When I try to put a prefix on the second form it just generates one id then if add more than one item it will save the last item.
I would like to know how to fix this problem and why it happened ?
thanks in advance.
my forms.py
class ResearchLineForm(forms.ModelForm):
class Meta:
model = ResearchLines
fields = ('title', )
widgets = {
'title':forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Linhas de Pesquisa'
})
}
LinesFormSet = formset_factory(ResearchLineForm)
class TokenForm(forms.Form):
name = forms.CharField( max_length=255, widget=forms.TextInput(
attrs={
'class':'form-control',
'placeholder':'Linhas de Pesquisa'
})
)
email = forms.CharField(max_length=255, widget=forms.TextInput(
attrs={
'class':'form-control',
'placeholder':'Linhas de Pesquisa'
})
)
TokenFormSet = formset_factory(TokenForm)
my view.py
def ubc_register(request):
if request.method == "POST":
ubc = Ubc()
form = UbcForm(request.POST)
tokenformset = TokenFormSet(request.POST, request.FILES)
formset = LinesFormSet(request.POST, request.FILES)
if form.is_valid():
ubc.name = form.cleaned_data['name']
ubc.laboratory_departament = form.cleaned_data['laboratory_departament']
ubc.partner_category = form.cleaned_data['partner_category']
ubc.parent_institution_name = form.cleaned_data['parent_institution_name']
ubc.coordinator = form.cleaned_data['coordinator']
if "phone" in form.cleaned_data:
ubc.phone = form.cleaned_data['phone']
if "email" in form.cleaned_data:
ubc.email = form.cleaned_data['email']
if "cnpj" in form.cleaned_data:
ubc.cnpj = form.cleaned_data['cnpj']
if "rad_operating_time" in form.cleaned_data:
ubc.rad_operating_time = form.cleaned_data['rad_operating_time']
if "rh_count" in form.cleaned_data:
ubc.rh_count = form.cleaned_data['rh_count']
if "website" in form.cleaned_data:
ubc.website = form.cleaned_data['website']
ubc.cep = form.cleaned_data['cep']
ubc.street = form.cleaned_data['street']
ubc.city = form.cleaned_data['city']
ubc.neighborhood = form.cleaned_data['neighborhood']
ubc.number = form.cleaned_data['number']
ubc.uf = form.cleaned_data['uf']
ubc.filter_grade = 0
ubc.grade = 0
if 'activity_branch' in form.cleaned_data:
ubc.activity_branch = form.cleaned_data['activity_branch']
ubc.save()
# research_lines
if formset.is_valid():
for researchline in formset.cleaned_data:
if bool(researchline):
created_researchline = ResearchLines.objects.create(title=researchline['title'])
ubc.research_line.add(created_researchline)
product_ids = [ cert.id for cert in form.cleaned_data["main_product"] ]
for main_product in MainProductUbc.objects.filter(id__in=product_ids):
ubc.main_product.add(main_product)
)
return HttpResponseRedirect(reverse('index'))
else:
form = UbcForm()
formset = LinesFormSet()
tokenformset = TokenFormSet()
context = {
'form':form,
'formset':formset,
'tokenformset': tokenformset,
}
return render(request, 'accounts/ubc_form.html', context)
my formset.js file
$('.link-formset').formset({
deleteText: 'Remover'
});
formsets use #id_form-TOTAL_FORMS (and a few other things) to see how many forms there are and were initialy.
If you want to display more than one formset on a single page you'll run into the problem that you end up with two of these objects with the same id. To solve this you can define a prefix for one (or both) of the formsets.

Categories