不会使用现有数据填充字段

时间:2014-03-21 17:52:16

标签: django django-models django-forms

当我访问更新页面时,它不会使用现有的条目数据填充字段(就在那里,我在视图的某些部分放置的打印语句显示它可以访问<存在),我不确定为什么它没有填充。

这是我的观点:

@login_required
def sites_update_view(request, place_id=None):

    if place_id:
        place = get_object_or_404(SiteMeta, pk=place_id)
    else:
        return redirect('sites-index')

    if request.POST:
        form = SitesAddForm(request.POST, instance=place)
        if form.is_valid():
            form.save()
            return redirect('sites-index')
    else:
        form = SitesAddForm(instance=place)

    return render(request, 'sites-update.html', {
        'form': form,
        'site': place,
        'place_id': place_id
    })

我的模板:

{% extends "newbase.html" %}
{% load url from future %}
{% load floppyforms %}
{% load staticfiles %}
{% block title %} - Update Site {% endblock %}
{% block content %}
<div class="row">
<div class="col-sm-6 col-md-6">
    <h3 class="heading">Update Surveillance Site</h3>
    <form method="post" action={% url 'sites-update' place_id=site.pk %}>
        {% csrf_token %}
        <div class="formSep">
            <div class="row">
                <div class="col-sm6 col-md-6">
                <label for="id_name">Site Name:<span class="f_req">*</span></label>
                    {{ form.name }}
                <span class="help-block">What is the site name?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_lga">LGA:<span class="f_req">*</span></label>
                    {{ form.lga }}
                <span class="help-block">What is the LGA?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_site_type">Site Type:<span class="f_req">*</span></label>
                    {{ form.site_type }}
                <span class="help-block">What type of site is this?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_site_priority">Site Priority:<span class="f_req">*</span></label>
                    {{ form.site_priority }}
                <span class="help-block">What is the priority of this site?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_site_category">Site Category:<span class="f_req">*</span></label>
                    {{ form.site_category }}
                <span class="help-block">What category should the site be in?</span>
                </div>
            </div>
        </div>
        <div class="row">
            <input class="btn btn-default" type="submit" value="Save" />
            <a class="btn btn-default" href={% url "sites-index" %}>Cancel</a>
        </div>
    </form>
</div>
</div>
{{ form.errors }}
{% endblock %}
{% block sidebar %}
    {% include "afp-sidebar.html" %}
{% endblock %}

1 个答案:

答案 0 :(得分:1)

if request.POST:应为if request.method == 'POST':