I'm trying to acomplish the following functionality: the user enters required information in the form created using generic CreateView, in this case it is length and width. After the form is submitted the app performs simple calculations and returns the result along with submitted data. I tried to follow this example: [https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views
My models:
from django.db import models
class Building(models.Model):
length = models.IntegerField(default=1)
width = models.IntegerField(default=1)
area = models.IntegerField(default=1)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
self.area = self.length * self.width
super().save(*args, **kwargs)re
views:
class BuildingDetailView(DetailView):
model = Building
context_object_name = 'building'
class BuildingCreateView(CreateView):
model = Building
form_class = BuildingForm
success_url = reverse_lazy('building-detail')
class BuildingListView(ListView):
model = Building
class BuildingUpdateView(UpdateView):
model = Building
form_class = BuildingForm
success_url = reverse_lazy('building_detail')
urls:
path('', views.indexView, name='index'),
path('add_temp/', views.BuildingCreateView.as_view(), name='building_add'),
path('buildings/', views.BuildingListView.as_view(), name='building_changelist'),
path('building/<int:pk>/', views.BuildingDetailView.as_view(), name='building-detail'),
html:
{% extends 'base.html' %}
{% block content %}
<h1>Title: </h1>
{% for object in object_list %}
<p><strong>Length:</strong> {{ object.length }}</p>
<p><strong>Width:</strong> {{ object.width }}</p>
<p><strong>Area:</strong> {{ object.area }}</p>
{% endfor %}
{% endblock %}
This setup is giving me the following error. I assume the problem is in my template:
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py" in post
172. return super().post(request, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py" in post
142. return self.form_valid(form)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py" in form_valid
126. return super().form_valid(form)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py" in form_valid
57. return HttpResponseRedirect(self.get_success_url())
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py" in get_success_url
112. if self.success_url:
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py" in __wrapper__
151. res = func(*self.__args, **self.__kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\base.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
673. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /hr/add_temp/
Exception Value: Reverse for 'building-detail' with no arguments not found. 1 pattern(s) tried: ['hr\\/building\\/(?P<pk>[0-9]+)\\/$']
Override the get_success_url(...)
method,
class BuildingCreateView(CreateView):
model = Building
form_class = BuildingForm
def get_success_url(self):
return reverse_lazy('building-detail', kwargs={'pk': self.object.pk})
class BuildingUpdateView(UpdateView):
model = Building
form_class = BuildingForm
def get_success_url(self):
return reverse_lazy('building-detail', kwargs={'pk': self.object.pk})