user1086337
user1086337

Reputation: 397

Doesn't populate fields with existing data

When I access the update page it doesn't populate the fields with the existing entry data (which is there, and print statements I've placed in parts of the view show that it's accessible and exists), I'm not really sure why it's not populating.

This is my view:

@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
    })

My template:

{% 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 %}

Upvotes: 1

Views: 85

Answers (1)

teewuane
teewuane

Reputation: 5734

if request.POST: should be if request.method == 'POST':

Upvotes: 1

Related Questions