反转 '' 没有找到参数 '('',)'
Reverse for '' with arguments '('',)' not found
我正在尝试实现一种方法来更改我的应用程序上的一些已保存设置,用户应该能够相应地查看和更改这些设置。
但是,在尝试加载“设置主页”页面时出现上述错误。
请查看下面的代码和完整的错误信息:
错误:
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
代码
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' ),
我认为 'pk' 实现有问题,但我就是想不通。
如果有人发现此代码中有任何错误,请提供帮助。
这是您的 settinghome.html
中的问题
<a href="{% url 'viewSettings' settings.id %}">» {{ entry.Complex }}</a>,
呈现 settinghome.html 的视图不期望您在 ViewSetting
url.
上调用的 pk
更新
使用entry.id
代替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 %}
我正在尝试实现一种方法来更改我的应用程序上的一些已保存设置,用户应该能够相应地查看和更改这些设置。
但是,在尝试加载“设置主页”页面时出现上述错误。
请查看下面的代码和完整的错误信息:
错误:
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 7Reverse 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
代码
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' ),
我认为 'pk' 实现有问题,但我就是想不通。
如果有人发现此代码中有任何错误,请提供帮助。
这是您的 settinghome.html
<a href="{% url 'viewSettings' settings.id %}">» {{ entry.Complex }}</a>,
呈现 settinghome.html 的视图不期望您在 ViewSetting
url.
更新
使用entry.id
代替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 %}