Django.urls.exceptions.NoReverseMatch URLS path seem to be correct
Normally this would be a simple problem to solve and perhaps I'm missing something very basic. But I have been pounding my head against this problem all morning.
I receive the error message:
django.urls.exceptions.NoReverseMatch: Reverse for 'journalrep' with arguments '('',)' not found. 2 pattern(s) tried: ['reports/journalrep/(?P<column>[^/]+)/(?P<direction>[^/]+)\\Z', 'reports/journalrep/\\Z']
I the debug log of my application.
My urls.py contains:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='reports'),
path('sumlist/', views.summary_list,name='sumlist'),
path('overallsummary',views.overallsummary,name='overallsummary'),
path('checkreg', views.checkreg, name='checkreg'),
path('checkdet/<chkno>/', views.checkdet, name='checkdet'),
path('journalrep/', views.journalrep, name='journalrep'),
path('journalrep/<column>/<direction>', views.journalrep, name='journalrep'),
path('journaldet/<tranid>', views.journaldet, name='journaldet'),
path('accountrep', views.accountrep, name='accountrep')
]
The view that renders the template is a function view:
@login_required
def journalrep(request,column = 'date', direction = 'D'):
'''
Produce journal register
Will display information for a chart of accounts account if provided. If the value is 0 all
journal entries will be shown
'''
#
# Get list of accounts (Chart of acconts) to be used for account selection box
coa = ChartOfAccounts.objects.all().filter(COA_account__gt=0)
coa_account = request.session.get('coa_account', None)
if len(request.GET) != 0:
coa_account = request.GET.get('coa_account')
else:
if coa_account == None:
coa_account = '0'
if direction == 'D':
direction = '-'
else:
direction = ""
if coa_account == '0':
journal = Journal.objects.all().order_by(direction + column)
else:
journal = Journal.objects.filter(account__COA_account = coa_account).order_by(direction + column)
context = { 'coa' : coa, 'journal' : journal , 'coa_account' : Decimal(coa_account)}
request.session['coa_account'] = coa_account
return render(request, 'reports/journal.html', context)
And the template that is rendered is:
<div class="container shadow min-vh-100 py-2">
<h2>Journal Register</h2>
<select name="coa_account" hx-get="{% url 'journalrep' row.transactionID %}" hx-target="#requestcontent" >
<option value="0">All</option>
{% for option in coa %}
<option value="{{option.COA_account}}"
{% if option.COA_account == coa_account %} selected {% endif %}>
{{option.COA_account_name}}
{% if option.COA_account_subgroup != "" %}
- {{option.COA_account_subgroup}}
{% endif %}
</option>
{% endfor %}
</select>
<div class="table-responsive">
<table class="table table-hover ">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Date <br>
<a hx-get="{% url 'journalrep' 'date' 'D' %}" hx-target="#requestcontent" >
<i class="bi bi-sort-alpha-down"> </i>
</a>
<a hx-get="{% url 'journalrep' 'date' 'A' %}" hx-target="#requestcontent" >
<i class="bi bi-sort-alpha-up" ></i>
</a>
</th>
<th scope="col">Account<br>
</th>
<th scope="col">Description<br> </th>
<th scope="col">Amount<br>
<a hx-get="{% url 'journalrep' 'amount' 'D' %}" hx-target="#requestcontent" >
<i class="bi bi-sort-alpha-down"> </i>
</a>
<a hx-get="{% url 'journalrep' 'amount' 'A' %}" hx-target="#requestcontent" >
<i class="bi bi-sort-alpha-up" ></i>
</a>
</th>
</tr>
</thead>
<tbody>
{% for row in journal %}
<tr data-bs-toggle="collapse" data-bs-target="#detail-">
<th scope="row">
{% if request.user.is_superuser %}
<button hx-get="{% url 'journaldet' row.transactionID %}" hx-target="#dialog" >
<i class="bi bi-eye"></i>
</button>
{% else %}
{% endif %}
</th>
<td>{{ row.date }}</td>
<td>
{{ row.account.COA_account}}<br>
{{ row.account.COA_account_name}}
</td>
<td>
{{ row.description }}
{% if row.transactionID != "" %}
<br>{{ row.transactionID}}
{% endif %}
</td>
<td align="right">${{ row.amount | floatformat:2 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div id="modal" class="modal fade">
<div id="dialog" class="modal-dialog" hx-target="this"></div>
</div>
<script>
const modal = new bootstrap.Modal(document.getElementById("modal"))
htmx.on("htmx:afterSwap", (e) => {
// Response targeting #dialog => show the modal
if (e.detail.target.id == "dialog") {
modal.show()
}
})
</script>
Try using this instead:
{% url 'journalrep' column='date' direction='D' %}
And also in urls.py:
path('journalrep/<str:column>/<str:direction>', views.journalrep, name='journalrep')
And potentially removing the line above this also as I'm not sure its required.
It's possible that django is arching the first one, but hard to say with the information provided.
In this line (line 3 of the template)
<select name="coa_account" hx-get="{% url 'journalrep' row.transactionID %}" hx-target="#requestcontent" >
You are not, at that point, looping through rows, so the value row.transactionID is empty, creating the empty argument of the error.
Based on what occurs later and your urls.py , you are prabably also wanting to reference journaldet rather than journaldep for that URL structure to work.