Francisco Colina
Francisco Colina

Reputation: 399

Problems with a Form to Edit a Model in Django - Reverse for 'variable name' not found

I'm currently having an issue to display a form, the following code is a depiction of the problem:


views.py

class LeadUpdate(UpdateView):
    model = Leads
    fields='__all__'
    template_name_suffix = '_update_form'

urls.py

from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('', views.dashboard, name='dashboard'),
    path('', include('django.contrib.auth.urls')),
    path('edit/', views.edit, name='edit'),
    path('add_client/', views.add_client, name='add_client'),
    path('add_company/', views.add_company,name='add_company'),
    path('add_lead/', views.add_lead, name='add_lead'),
    path('add_call/', views.add_call, name='add_call'),
    path('add_status/', views.add_status, name='status'),
    path('leads_view/', views.leads_view, name='leads_view'),
    path('client_view/', views.client_view, name='client_view'),
    path('deal_view/', views.deal_view, name='deal_view'),
    path('<int:project_id>/',views.LeadUpdate.as_view(), name='edit_lead')
]

The problem I have corresponds to the last URL

The dashboard displaying the list and the web app is design such that when the user clicks the number you can edit the form dashboard.html

    <table class="styled-table">
        <thead>
            <tr>
                <th>Project ID</th>
                <th>Agent</th>
                <th>Company</th>
                <th>Country</th>
                <th>Modelling Services</th>
                <th>Est. Closing Date</th>
                <th>Est. Revenue</th>
                <th>Est. # of Licenses</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
        {% for lead in leads %}
            <tr>
                <td><a href="{% url 'project_id' lead.project_id %}">{{lead.project_id }}</a></td>
                <td>{{ lead.agent }}</td>
                <td>{{ lead.company }}</td>
                <td>{{ lead.country }}</td>
                <td>{{ lead.modeling_services }}</td>
                <td>{{ lead.estimated_closing_date }}</td>
                <td>{{ lead.expected_revenue }}</td>
                <td>{{ lead.expected_licenses }}</td>
                <td>{{ lead.status }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>


This is the file edit_lead.html

{% extends "base.html" %}

{% block title %}Client Information {% endblock %}

{% block content %}
  <h1> Edit Lead </h1>
  <p>Please use the form below to edit your lead</p>

    <form method="post" enctype="multipart/form-data">
      {% csrf_token %}
    <table>{{ form.as_p }} </table>

    <p><input type="submit" value="Save changes"></p>
    </form>


{% endblock %}

This is the error: NoReverseMatch at /account/ Reverse for 'project_id' not found. 'project_id' is not a valid view function or pattern name. Request Method: GET Request URL: http://127.0.0.1:8000/account/ Django Version: 3.2.5 Exception Type: NoReverseMatch Exception Value:
Reverse for 'project_id' not found. 'project_id' is not a valid view function or pattern name. Exception Location: C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix Python Executable: C:\Users\fcolina\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe Python Version: 3.9.7 Python Path:
['C:\Users\fcolina\PycharmProjects\crmdb', 'C:\Program ' 'Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\python39.zip', 'C:\Program ' 'Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\DLLs', 'C:\Program ' 'Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib', 'C:\Users\fcolina\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0', 'C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages', 'C:\Program ' 'Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0', 'C:\Program ' 'Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\site-packages'] Server time: Sun, 05 Sep 2021 20:23:33 +0000

Upvotes: 1

Views: 129

Answers (1)

jmcarson
jmcarson

Reputation: 446

The django url template tag takes the urlpattern name as the first argument. You should update to:

<td><a href="{% url 'edit_lead' lead.project_id %}">{{lead.project_id }}</a></td>

https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#url

Upvotes: 2

Related Questions