直接从 CreateView 访问 Django 通用 DetailView

Accessing Djnago generic DetialView directly from CreateView

我正在尝试完成以下功能:用户在使用通用 CreateView 创建的表单中输入所需信息,在本例中为长度和宽度。提交表单后,应用程序会执行简单的计算,并 returns 结果连同提交的数据。 我试着按照这个例子:[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')

网址:

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]+)\/$']


覆盖get_success_url(...)方法,

class BuildingCreateView(CreateView):
    model = Building
    form_class = BuildingForm

    <b>def get_success_url(self):
        return reverse_lazy('building-detail', kwargs={'pk': self.object.pk})</b>


class BuildingUpdateView(UpdateView):
    model = Building
    form_class = BuildingForm

    <b>def get_success_url(self):
        return reverse_lazy('building-detail', kwargs={'pk': self.object.pk})</b>

reverse_lazy('building-detail') 将不起作用,因为 "building-detail" 需要参数 "pk"。您有 2 个选择:

覆盖视图中的 get_success_url 方法

def get_success_url(self):
    return reverse('building-detail', kwargs={'pk': self.object.pk})

或向您的 Building 模型添加一个 get_absolute_url 方法,默认情况下所有 update/create 视图都会使用该方法,不需要添加 success_urlget_success_url对您的看法

def get_absolute_url(self):
    return reverse('building-detail', kwargs={'pk': self.pk})