update a single record with submit in index.html and function in views.py

1

I have a code which should allow me to update the field = status, by 'delivered' this is the index.html where the button is:

<form action="/some/url/mapped/to/pedido_sub/view/">
      <input type="submit" name="Aprobar" class="btn btn-primary pull-right" value="Aprobar"/>
</form>

then the views.py code is this:

def pedido_sub(request, id_pedido):

    pedido = get_object_or_404(Pedido, pk=id_pedido)
    if (request.method == POST) and ("Aprobar" in request.POST):
        pedido.estado = 'entregado'
        pedido.save()
        # Send a Success Message to the User
    else:
        return render(request, 'index.html')

a idea is that when I press the button I update the field, but the error is:

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/some/url/mapped/to/pedido_sub/view/?Aprobar=Aprobar

My urls.py of the app:

urlpatterns = [ 
    url(r'^home', login_required(Pedidoapp.views.home), name='home'), 
    url(r'^confirmar/',login_required(Pedidoapp.views.aprobar_pedido), name='aprobar_pedido'),
    url(r'^Pedido/$', login_required(Pedidoapp.views.add), name="add"),

My project urls.py:

urlpatterns = [ # Examples: 
    url(r'^home/', include(Pedidoapp.urls, namespace="usuario")),
    url(r'^home/$', login_required(Pedidoapp.views.home), name="home"), 
    # url(r'^home_user/$', login_required(Pedidoapp.views.homeuser), name="homeuser"), 
    url(r'^add/$', login_required(Pedidoapp.views.add), name="add"), url(r'aprobar_pedido/$', aprobar_pedido, name='aprobar_pedido'),
    
asked by Demaro Create 14.02.2017 в 16:20
source

1 answer

1

I think you're confusing several of Django's concepts:

1) - The form is directing to a route ( /some/url/mapped/to/pedido_sub/view/ ) that is not specified in urls.py of the app. You should add the following line ( note that I changed /some/url/mapped/to/pedido_sub/view/ for /pedido_sub/ for simplicity for the example ):

 url(r'^pedido_sub/(?P<id_pedido>\d+)/$', Pedidoapp.pedido_sub , name="pedido_sub"),

There I am telling you that after the address pedido_sub/ a number will come. And the html of the form would be:

<form action="{% url 'pedido_sub' variable %}" method="GET">
      <input type="submit" name="Aprobar" class="btn btn-primary pull-right" value="Aprobar"/>
</form>

Note that the name declared in urls.py can be used to make the code more maintainable in the event that in the future you want to change the url without having to modify all the routes in your HTMLs.

2) - In order to use the id_pedido that you are receiving per parameter in pedido_sub(request, id_pedido) you would have to send the id by the URL, for example:

localhost/miPagina/pedido_sub/4783/

Where 4783 is the request_id. As I mentioned above it can be done:

<form action="{% url 'pedido_sub' id_pedido %}" method="GET">

Being a Django variable ordered_id.

3) - And finally you should take request.GET instead of request.POST since it does not seem to be sensitive information that you are sending and saves you from using the CSRF Token:

def pedido_sub(request, id_pedido):

    pedido = get_object_or_404(Pedido, pk=id_pedido)
    if (request.method == GET) and ("Aprobar" in request.GET):
        pedido.estado = 'entregado'
        pedido.save()
        # Send a Success Message to the User
    else:
        return render(request, 'index.html')

I hope I helped you! Anything you tell us!

Greetings!

    
answered by 14.02.2017 в 17:36