Hi everyone, I recently reviewed the off-django documentation and decided to master deleting data from the database using the POST method (I know that there is such a thing as Form in django, but I want to try it first myself). Actually when I load the page I get the following error:

Reverse for 'delete' with arguments '(' ',)' and keyword arguments '{}' not found. 1 pattern (s) tried: ['$ delete / $']

Here's the content: views.py from django.shortcuts import render from django.http import HttpResponseRedirect, HttpResponse from .models import News

from django.core.urlresolvers import reverse # Create your views here. def news(request): listNews = News.objects.all() context = {'listNews': listNews} return render(request, 'news/index.html', context) def delete(request): delNews = request.POST['news'] d = News.objects.filter(id=delNews).delete() return reverse('news:delete', kwargs={'news.id'}) 

urls.py

 from django.conf.urls import url from . import views app_name='news' urlpatterns = [ url(r'^$', views.news, name='news'), url(r'^delete/$', views.delete, name='delete'), ] 

index.html

 {% if listNews %} <form action="{% url 'news:delete' news.id %}" method="post"> {% for news in listNews %} {% csrf_token %} <h4>{{news.title}}</h4> {{news.text}} {{news.author}} {{news.date}} <input type="text" name="news" id="news{{ forloop.counter }}" value="{{ news.id }}" /> <input type="submit" /> {% endfor %} </form> <p>No news avaliable</p> {% endif %} 

PS I realize that there may be some other kind of mistakes here, I would be very grateful if you could help me.

    2 answers 2

    You have a link in the form action wants to be built using news.id, but in the URL it is not.

    The url tag builds links based on the lines you have written in urls.py.

    Remove news.id from the line {% url 'news:delete' news.id %}

    • removed, but the error still remained. - Konstantin
    • And show me how this urls.py connects with you. And then in the formation of the url is used namespace news :. Maybe the system simply does not see this namespace? - Konstantin Shishkin
    • from django.conf.urls import include, url from django.contrib import admin urlpatterns = [url (r '^ polls /', include ('polls.urls')), url (r' ^ $ ', include (' news .urls')), url (r '^ admin /', admin.site.urls),] <- here are the urls in the root folder - Konstantin
    • url (r '^ $', include ('news.urls'), namespace = "news") - try - Konstantin Shishkin
    • Thanks for the help, but I decided to rewrite the code. - Constantine

    Thanks for the help, but I decided to rewrite the code: Maybe views.py can be useful to someone :

      from django.shortcuts import get_object_or_404, render from django.http import HttpResponse from django.contrib.auth import authenticate from .models import News # Create your views here. def news(request): listNews = News.objects.all() context = {'listNews': listNews} return render(request, 'news/index.html', context) def delete(request, news_id): if request.user.is_authenticated(): if request.method == 'POST': emp = News.objects.get(pk = news_id) emp.delete() return HttpResponse('deleted') 

    index.html:

     {% for news in listNews %} <h4>{{news.title}}</h4> {{news.text}} {{news.author}} {{news.date}} {% if user.is_authenticated %} <form action='delete/{{news.id}}/' method='post'> {% csrf_token %} <input type="submit" value="Отправить" > </form> {% endif %} {% endfor %} 

    urls.py in the news project :

     from django.conf.urls import url from . import views app_name='news' urlpatterns = [ url(r'^$', views.news, name='news'), url(r'^delete/(?P<news_id>[0-9]+)/$', views.delete, name='delete'), ] 

    main urls.py:

     from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^polls/', include('polls.urls')), **url(r'^news/', include('news.urls')),** url(r'^admin/', admin.site.urls), ]