Реверс для 'company_delete' с аргументами '(' ',)' не найден. Попробован 1 шаблон (ов): ['company / (? P <pk>\\ d +) / delete / $'] - PullRequest
0 голосов
/ 19 февраля 2020

Я получаю:

django .urls.exceptions.NoReverseMatch: Реверс для 'company_delete' с аргументами '(' ',)' не найден. Попробован 1 шаблон (ов): ['company / (? P \ d +) / delete / $']

и в моей консоли он показывает:

- Не удалось загрузить ресурс: сервер ответил с состоянием 500 (Внутренняя ошибка сервера)

Мой views.py

def company_delete(request,pk):
    data = dict()
    company = get_object_or_404(Company,pk=pk)
    if request.method == "POST":
        company.delete()
        data['form_is_valid'] = True
        companies = Company.objects.all()
        data['company_list'] = render_to_string('company_list_2.html',{'companies': companies})
    else:
        context = {'company': company}
        data['html_form'] = render_to_string('company_delete.html',context,request=request)

    return JsonResponse(data)

мой urls.py

urlpatterns = [
    url(r'^company/$', views.company_list, name='company_list'),
    url(r'^company/create/$', views.company_create, name='company_create'),
    url(r'^company/(?P<pk>\d+)/update/$', views.company_update, name='company_update'),
    url(r'^company/(?P<pk>\d+)/delete/$', views.company_delete, name='company_delete'),
]

my company_list_2.html

{% for c in companies %}
  <tr>
    <td>{{ c.name }}</td>
    <td>{{ c.description }}</td>
    <td>{{ c.website }}</td>
    <td>{{ c.address }}</td>
    <td>{{ c.phone }}</td>
    <td>{{ c.email }}</td>
    <td>{{ c.contact }}</td>
    <td>
      <button class="btn btn-warning show-form-update" data-url="{% url 'company_update' c.pk %}">
        <span class="glyphicon glyphicon-pencil"></span>
        Edit
      </button>
    </td>
    <td>
      <button class="btn btn-danger show-form-delete" data-url="{% url 'company_delete' c.pk %}">
        <span class="glyphicon glyphicon-trash"></span>
        Delete
      </button>
    </td>
  </tr>
{% empty %}
  <tr>
    <td colspan="7" class="text-center bg-warning">No Company</td>
  </tr>
{% endfor %}

my company_delete.html

{% load crispy_forms_tags %}
<form method="post" data-url="{% url 'company_delete' c.pk %}" class="delete-form">
    {% csrf_token %}
    <div class="modal-header">
        <h3 class="modal-title" >Delete Company</h3>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        <p class="lead"> Are you sure you want to delete this company <strong>{{ c.name }}</strong></p>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-danger">Delete Company</button>
    </div>
</form>

1 Ответ

0 голосов
/ 19 февраля 2020

Как говорят @ Иван Старостин просто поместите company.pk экземпляр c.pk в свой URL-адрес

<form method="post" data-url="{% url 'company_delete' company.pk %}" class="delete-form">
    ...
</form> 
...