Initial Values in Django model form










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 %









share|improve this question
























  • 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 ?

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 12 '18 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.

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 13 '18 at 1:24















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 %









share|improve this question
























  • 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 ?

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 12 '18 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.

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 13 '18 at 1:24













0












0








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 %









share|improve this question
















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 %






django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 19:37







MattG

















asked Nov 12 '18 at 17:55









MattGMattG

1068




1068












  • 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 ?

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 12 '18 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.

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 13 '18 at 1:24

















  • 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 ?

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 12 '18 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.

    – Håken Lid
    Nov 12 '18 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.

    – MattG
    Nov 13 '18 at 1:24
















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 ?

– Håken Lid
Nov 12 '18 at 18:12






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 ?

– Håken Lid
Nov 12 '18 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.

– MattG
Nov 12 '18 at 19:26






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.

– MattG
Nov 12 '18 at 19:26





1




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.

– Håken Lid
Nov 12 '18 at 20:21






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.

– Håken Lid
Nov 12 '18 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.

– MattG
Nov 13 '18 at 1:24





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.

– MattG
Nov 13 '18 at 1:24












1 Answer
1






active

oldest

votes


















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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267598%2finitial-values-in-django-model-form%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer



























      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.






      share|improve this answer

























        2












        2








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 20:03









        Daniel RosemanDaniel Roseman

        447k41578635




        447k41578635



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267598%2finitial-values-in-django-model-form%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo