view based on django functions

0

I am making progress in the development of django with an application, initially with the use of function-based views, my idea is also to handle views based on classes, and I am with an application that allows me to recover a record from a table to From the ID of that table, the view is:

def recibo_edit(request, id_recibo):
    recibo = Recibo.objects.get(id=id_recibo)
    if request.method == 'GET':
        form = ReciboForm(instance=recibo)
    else:
        form = ReciboForm(request.POST, instance=recibo)
        if form.is_valid():
            form.save()
        return redirect('recibo:recibo_listar')
    return render(request, 'recibos/recibo_form.html', {'form': form})

While in the urls.py file of the application I receive, I have the following:

app_name ='recibos'

urlpatterns = {
    path('', index, name='index'),
    path('nuevo/', recibo_view, name='recibo_nuevo'),
    path('listar/', recibo_list, name='recibo_listar'),
    path('editar/(?P<id_recibo>\d+)/$', recibo_edit, name='recibo_editar'),
}

When I run the django server, and use the path to retrieve a record: the id = 365 that exists in the database, it returns the following error:

The current path, recibos/editar/365/, didn't match any of these

Apparently everything is fine, but I can not identify where the error is. Thank you in advance for your comments.

Thank you very much! Gustavo.

    
asked by Gustux 13.12.2017 в 19:50
source

1 answer

2

The error is caused because the path function introduced in Django 2.0 does not use regular expressions , in your case you should use:

path('editar/<int:id_recibo>/', recibo_edit, name='recibo_editar')

If you want to use a regular expression, you can use re_path () :

re_path('editar/(?P<id_recibo>\d+)/$', recibo_edit, name='recibo_editar')

Also the url() function still works and is now an alias for re_path, but it is likely to be deprecated in the future.

url(r'editar/(?P<id_recibo>\d+)/$', recibo_edit, name='recibo_editar')

Note: the application should give you the following warning:

WARNINGS: ?: (2_0.W001) Your URL pattern 'editar/(?P<id_recibo>\d+)/$' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().

I recommend you check the following link so you can see what's new in django 2.0

    
answered by 14.12.2017 в 05:20