[Answer]-Request 'GET' – Django CRUD conditional

1👍

You have some obvious bug in your code:

if Proyecto.objects.count() < 0:

count means an act of determining the total number of something. and total number of something can not be less than zero.

So your code is logically wrong. this bug also affect these lines:

    if Proyecto.objects.count() > 0:
        alerta="ya existe el proyecto"
        formProy = 1
        ctx ={'alerta':alerta, 'formProy':formProy}
        return render_to_response('scppp/inicial.html', ctx, context_instance=RequestContext(request))
    else:
        if Proyecto.objects.count() == 0:
            id_proyecto=1
        else:
            id_proyecto=Proyecto.objects.all().aggregate(Max('id_proyecto'))['id_proyecto__max']+1
        nombre_proyecto = name

Executation never reachs to else statement.

Also in your ** GET request ** processing in views there is another mistake.

if existente is not None and existente != '':
    Proyecto.objects.filter(nombre_proyecto=name)
    return render_to_response('scppp/pozo.html', ctx, context_instance=RequestContext(request))

you didn’t use selected Proyecto instance from database. You should pass them to your template. Also you didn’t declared ctx in this scope. (You just did it in POST request handling and a request can not be both of POST and GET).

if existente is not None and existente != '':
    projects = Proyecto.objects.filter(nombre_proyecto=name)
    ctx = {"projects" : projects}
    return render_to_response('scppp/pozo.html', ctx, context_instance=RequestContext(request))

and also in your scppp/pozo.html you must have something like this to show them and use projects:

<table>
    {% for project in projects %}
    <tr>
        <td>{{project.id_proyecto}}</td>
        <td>{{project.nombre_proyecto}}</td>
   </tr>
   {% endfor %}
</table>

Leave a comment