Я пытаюсь дополнить sh следующей функциональностью: пользователь вводит необходимую информацию в форму, созданную с использованием generi c CreateView, в данном случае это длина и ширина. После отправки формы приложение выполняет простые вычисления и возвращает результат вместе с отправленными данными. Я пытался следовать этому примеру: [https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views
Мои модели:
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
просмотров:
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')
URL:
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 %}
Эта установка выдает мне следующую ошибку. Я предполагаю, что проблема в моем шаблоне:
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]+)\\/$']