Issue
I am currently running into the above error when trying to render a Model Form.
My form looks like this at the moment:
Form.py:
class SettingUpdateForm(ModelForm):
class Meta:
model = SettingsClass
fields = '__all__'
Models.py:
Complex = models.CharField(choices=complex_list , max_length = 22 ,default='1' , unique=True)
#Trial Balance Year To Date
Trial_balance_Year_to_date= models.BooleanField(default = False)
tbytd_Include_opening_balances=models.BooleanField(default = False)
tbytd_Only_use_main_accounts=models.BooleanField(default = False)
tbytd_Print_null_values=models.BooleanField(default = False)
tbytd_Print_description=models.BooleanField(default = True)
tbytd_Print_account=models.BooleanField(default = True)
tbytd_Sort_by_account_name=models.BooleanField(default = True)
#Trial Balance Monthly
Trial_balance_Monthly=models.BooleanField(default = False)
tbm_Only_use_main_accounts=models.BooleanField(default = False)
tbm_Print_null_values=models.BooleanField(default = False)
tbm_Print_description=models.BooleanField(default = True)
tbm_Print_account=models.BooleanField(default = True)
tbm_Sort_by_account_name=models.BooleanField(default = True)
When the form is trying to render, it gives the following error based on my views.py:
Full Error Message:
NoReverseMatch at /accConnect/setting/2
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/accConnect/setting/2
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: Mon, 20 Sep 2021 06:35:19 +0000
Error during template rendering
In template C:\Users\KylePOG\Documents\GMA Programming\accConnect\main\templates\main\base.html, error at line 11
Reverse for 'viewSettings' with arguments '('',)' not found. 1 pattern(s) tried: ['accConnect/setting/(?P<settings_pk>[0-9]+)$']
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <style type="text/css">
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
18 /* The navigation menu links */
19 .sidenav a {
20 padding: 6px 8px 6px 16px;
21 text-decoration: none;
Views.py:
def viewSettings(request, settings_pk):
setting = get_object_or_404(SettingsClass, pk=settings_pk)
if request.method == 'GET':
form = SettingUpdateForm(instance=setting)
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
else:
form = SettingUpdateForm(request.POST, instance=setting)
if form.is_valid():
form.save()
return redirect('settingsHome')
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
I have tried to replace the form... with the setting... in my HTML document which shows the following :
viewSetting.HTML
<form method="POST" action="{% url 'viewSettings' settings.pk %}">
{% csrf_token %}
<div class="container">
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Trial_balance_Year_to_date }} Trial Balance Year To Date</li>
<li class='list-group-item'>{{ form.tbytd_Include_opening_balances }} Include Opening Balances</li>
<li class='list-group-item'>{{ form.tbytd_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.tbytd_Print_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.tbytd_Print_description }} Print Description</li>
<li class='list-group-item'>{{ form.tbytd_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.tbytd_Sort_by_account_name }} Sort By Account Name</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Trial_balance_Monthly }} Trial Balance Monthly</li>
<li class='list-group-item'>{{ form.tbm_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.tbm_Print_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.tbm_Print_description }} Print Description</li>
<li class='list-group-item'>{{ form.tbm_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.tbm_Sort_by_account_name }} Sort By Account Name</li>
</ul>
</div>
<br>
<br>
<hr>
<div class="btn-update" style="padding-left: 1%">
<button type="submit" class='btn btn-primary'>Update</button>
<button type="delete" class="btn btn-danger">Delete</button>
</div>
</form>
Urls.py:
from django.contrib import admin
from django.conf.urls import url
from django.urls import path , include
from accounts import views as v
from main import views as views
urlpatterns = [
path('admin/', admin.site.urls),
# Accounts
path('login/home/' , views.home , name = 'home'),
path('', v.register , name='register'),
path('' , include('django.contrib.auth.urls') , name = 'login'),
#Trial Balance
path('KyletrbSettings' , views.KyletrbSettings , name = 'KyletrbSettings'),
path('Kyletrb', views.Kyletrb , name = 'Kyletrb'),
path('KyletrbMonth', views.KyletrbMonth , name = 'KyletrbMonth'),
path('pdf' , views.printToPdf, name='printToPdf'),
path('XLS' , views.printToXLS , name='printToXLS'),
path('trb_Monthly' , views.trb_Monthly , name='trb_Monthly'),
#Settings
path('settings', views.settingsHome , name='settingsHome'),
path('ns' , views.newSetting , name='newSetting'),
path('accConnect/setting/<int:settings_pk>', views.viewSettings, name='viewSettings' ),
]
I can't seem to find what the problem with this could be, I have tried removing the "action" part of the form tag and that renders the form however nothing is saved to the database when the "update" button is pressed and the page does not redirect as it is supposed to
Solution
Here is the code causing the problem
<form method="POST" action="{% url 'viewSettings' settings.pk %}">
In your viewsettings.html template replace settings.pk
with setting.pk
so you should have this <form method="POST" action="{% url 'viewSettings' setting.pk %}">
In your views.py
you are passing setting as the object in the context but in the template you are using settings thus the error
Update
To check what is causing the form not to be submitted you do this
def viewSettings(request, settings_pk):
setting = get_object_or_404(SettingsClass, pk=settings_pk)
if request.method == 'GET':
form = SettingUpdateForm(instance=setting)
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
else:
form = SettingUpdateForm(request.POST, instance=setting)
if form.is_valid():
form.save()
return redirect('settingsHome')
else: #add this to your code
print(form.errors)
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
Answered By - Mugoya Dihfahsih
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.