Django.fun

Reverse for '' with arguments '('',)' not found

I am trying to implement a way to change some saved settings on my app , the user should be able to view and change these settings accordingly.

However , the above error is received when trying to load the "SettingsHome" page.

Please see the below code and full error message:

Error:

NoReverseMatch at /settings Reverse for 'viewSettings' with arguments '('',)' not found. 1 pattern(s) tried: ['accConnect/setting/(?P<settings_pk>[0-9]+)$'] Request Method: GET Request URL: http://localhost:8000/settings Django Version: 3.2 Exception Type: NoReverseMatch Exception Value: Reverse for 'viewSettings' with arguments '('',)' not found. 1 pattern(s) tried: ['accConnect/setting/(?P<settings_pk>[0-9]+)$'] Exception Location: C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix Python Executable: C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\python.exe Python Version: 3.9.4 Python Path:
['C:\Users\KylePOG\Documents\GMA Programming\accConnect', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages'] Server time: Tue, 14 Sep 2021 08:30:03 +0000 Error during template rendering In template C:\Users\KylePOG\Documents\GMA Programming\accConnect\main\templates\main\base.html, error at line 7

Reverse for 'viewSettings' with arguments '('',)' not found. 1 pattern(s) tried: ['accConnect/setting/(?P<settings_pk>[0-9]+)$'] 1 2 3 4 5 /* The sidebar menu / 6 .sidenav { 7
height: 100%; /
Full-height: remove this if you want "auto" height / 8 width: 160px; / Set the width of the sidebar / 9
position: fixed; /
Fixed Sidebar (stay in place on scroll) / 10
z-index: 1; /
Stay on top / 11 top: 0; / Stay at the top / 12 left: 0; 13 background-color: #111; / Black / 14
overflow-x: hidden; /
Disable horizontal scroll */ 15
padding-top: 20px; 16 } 17

CODE

Views.py:

def settingsHome(request):

    allClass = SettingsClass.objects.all().order_by('Complex')
    return render(request, 'main/settingsHome.html' , {'allClass' : allClass})

def viewSettings(request,  settings_pk):
    setting = get_object_or_404(SettingClass, pk=settings_pk)
    if request.method == 'GET':
        form = SettingForm(instance=setting)
        return render(request, 'meter_readings/viewSettings.html', {'setting': setting, 'form':form})
    else:
        try:
            form =  SettingForm(request.POST, instance=setting)
            form.save()
            return redirect('settingsHome')
        except ValueError:
            return render(request, 'meter_readings/viewSettings.html', {'setting': setting, 'form':form, 'error':'Bad info'})

viewSettings.html:

{% extends "main/base.html"%}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

{% block content %}

{{ error }}

 {{ setting.Complex }}

<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Update</button>
</form>

{% endblock %}

settingHome.html:

{% extends "main/base.html"%}


{% block content %}
{% for entry in allClass %}

<div class="container">
  <div class="row mx-lg-n5">
    <a href="{% url 'viewSettings' settings.id %}">»  {{ entry.Complex }}</a>
  </div>
</div>

{% endfor %}

<br>

<a href="{% url 'newSetting' %}" class="btn btn-primary">➕ Add New Setting</a>

{% endblock %}

Urls.py:

#Settings
path('settings', views.settingsHome , name='settingsHome'),
path('ns' , views.newSetting , name='newSetting'),
path('accConnect/setting/<int:settings_pk>', views.viewSettings, name='viewSettings' ),

I think there is something wrong with the 'pk' implementation, but I just can't figure it out.

If anyone can see anything wrong in this code, please assist.

Answers: 1

Answered by Mugoya Dihfahsih, Sept. 14, 2021, 11:50 a.m.

Here is the problem in your settinghome.html

 <a href="{% url 'viewSettings' settings.id %}">» {{ entry.Complex }}</a>,

The view that renders settinghome.html does not expect pk which you are calling on ViewSetting url.

Update

use entry.id instead of settings.id

{% for entry in allClass %}

<div class="container">
  <div class="row mx-lg-n5">
    <a href="{% url 'viewSettings' entry.id %}">»  {{ 
 entry.Complex }}</a> <!--replace setting.id with entry.id -->
  </div>
</div>
{% endfor %}