0

I am having a difficult time understanding the Django docs on this one. I have also come across some other threads with the same question, but I cannot seem to get the suggested answers to work for me. I think it is because I am posting text, and it is not considered "clean" data?

I want to autofill two form fields, then when the user hits the submit button, it saves. But for some reason only the boolean value is working, not the text value. Any ideas?

You will also see the fields are hidden from my template. When I show these fields, the initial values are set correctly as I expected, but when I hit submit, only the boolean is saved to database correctly.

EDIT It works fine when I don't hide the form fields using {{ form }} in my template. It does not work when I hide the fields using {{ form.field.as_hidden }} Boolean field is accepted, but the text field is not. I am trying to autofill this field with a text value, hide it, and submit this value when the submit button is pressed...

views.py

class BuildStopView(LoginRequiredMixin,UpdateView):
    model = Build
    form_class = StopBuild
    template_name = 'build_stop.html'
    login_url = 'login'

forms.py

class StopBuild(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(StopBuild, self).__init__(*args, **kwargs)
        self.initial['buildEndType'] = 'manuallyStopped'
        self.initial['buildActive'] = False
    class Meta:
        model = Build
        fields =  ['buildEndType','buildActive']

(template) stop_build.html

{% extends 'base.html' %}

{% block body %}

<style>
  div.a {
      text-align: center;
  }
</style>

<div class = "a">
  <h3>Are you sure you want to stop this build manually?</h3>

</div>

<form action="" method="post">{% csrf_token %}
  {{ form.field.as_hidden }}
  <button class="btn btn-danger ml-2" type="submit">Stop Build Manually</button>
</form>

{% endblock %}
4
  • What do you expect {{ form.field.as_hidden }} to do? Have you tried simply rendering the form by using the template tag {{ form }} in that position instead ? Commented Nov 12, 2018 at 18:12
  • {{ form.field.as_hidden }} hides the form fields. I only want the user to see the button, not what is being submitted. It's basically a confirmation page. When I put {{ form }} it shows the fields. Commented Nov 12, 2018 at 19:26
  • 1
    Ok. The problem is that there's no attribute form.field, as explained by Daniel Roseman in his answer below. One thing I don't quite understand is why you are using a form at all here, if you want to hide every single field? Or is that just in the example code? If it's just a button, you don't need a form. You can have a POST with an empty payload. You are just passing data to the client, that you want the client to pass right back without modifying. Commented Nov 12, 2018 at 20:21
  • I thought there must be better way but wasn't sure what the best method was. I am showing the user the button which says "stop build" and for a "build" to be stopped with my code it needs to be set to False, so I thought I could just hide this actual field so it looks cleaner.. A POST sounds more correct and less hacky. Commented Nov 13, 2018 at 1:24

1 Answer 1

2

form.field.as_hidden does not output the fields as hidden, in fact it does not do anything at all because you don't have a field called field in your form. You need to refer to the actual fields:

{{ form.buildEndType.as_hidden }}
{{ form.buildActive.as_hidden }}

However, if you want these to always be shown as hidden, you should probably do it in your form definition, by declaring them with HiddenInput widgets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.