I have a form with an hidden input called social_color
where I want nothing in, I need this input to assign a value after validating the form content to use one of the field content to do stuff and then assign the results to the hidden input.
According to this answer: https://stackoverflow.com/a/813474/15352101
I can simply do :
if form.is_valid():
form.cleaned_data['Email'] = GetEmailString()
but for me using :
form.cleaned_data["social_color"] = icon_main_color
icon_main_color
is the results of the stuff I had to do, it's a tuple containing 3 values but when I check the created object with the form, the value of social_color
is still empty.
EDIT:
concerned view
@method_decorator(ajax_required, name="get")
class SocialCreationView(View):
ctx = {
"form": SocialCreationForm(),
}
def get(self, request, *args, **kwargs):
return render(request, "social_creation.html", self.ctx)
def post(self, request, *args, **kwargs):
payload = json.loads(request.body.decode())
form = SocialCreationForm(payload)
if form.is_valid():
# Quick binds
social_name = form.cleaned_data["social_name"]
social_icon = form.cleaned_data["social_icon"]
file_extention = None
#blabla
icon_main_color = get_colors(icon_save_path)[0]
if icon_main_color is not None:
form.cleaned_data["social_color"] = icon_main_color
form.save()
return redirect("/")
self.ctx["form"] = form
return render(request, "social_creation.html", self.ctx)
EDIT2:
SocialProfile
model:
class SocialProfile(models.Model):
social_name = models.CharField(default="Unnamed social profile", max_length=100)
social_username = models.CharField(default="Unknow username", max_length=16)
social_icon = models.CharField(default="path/to/social/icon", max_length=250)
social_link = models.CharField(default="https://social.nepmia.fr/", max_length=100)
social_color = models.CharField(max_length=10)