Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

django - Form Not Working After Converting to Bootstrap

I've got the following form that's fully working, but is just basic and ugly:

{{ form.non_field_errors }}
<form method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

When I try to convert it to a bootstrap form, like below, the Submit button does not actually submit changes. Any idea why this isn't working? When I hit Submit the values display as None, and when I go back to my selection screen the edits are not updated (they are also not updated in the backend database). Any idea what's wrong?

{{ form.non_field_errors }}
<form method="post">{% csrf_token %}
    <div class="form-group">
        {{ form.stakeholder_group.errors }}
        <label for="{{ form.stakeholder_group.id_for_label }}">Stakeholder Group</label>
        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add a Stakeholder Group"  value="{{ form.stakeholder_group.value }}">
        <small id="emailHelp" class="form-text text-muted">What group(s) is this invdividual associated with?</small>
    </div>

    <div class="form-group">
        {{ form.employee.errors }}
        <label for="{{ form.employee.id_for_label }}">Employee Name</label>
        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Log the employee" value="{{ form.employee.value }}">
        <small id="emailHelp" class="form-text text-muted">Who does this individual report to?</small>
    </div>
    <input type="submit" value="Submit"/>
</form>

Updated:

<form method="post">{% csrf_token %}

    {% for field in form %}
    <div class="form-group">
        {{ field.errors }}
        {{ field.label_tag }}
        <label for="{{ form.stakeholder_group.id_for_label }}"></label>
        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add a Stakeholder Group"  value="{{ field.value }}"> {{ field }}
        <small id="emailHelp" class="form-text text-muted">What group(s) is this invdividual associated with?</small>
    </div>
    {% endfor %}
    <input type="submit" value="Submit"/>

</form>

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

your form fields are missing. for e.g. if one if the fields is stakeholder_name then you need to add {{form.stakeholder_name}}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...