我有两个模型叫做customer
and building
。如何使用这两个模型在一个模板中使用基于类的通用视图进行通用查看。
这是我的urls.py来查看我的客户
urlpatterns = patterns('',
url(r'^customer/', customerView.as_view(), name= "customer_view"),
)
这是我给客户的view.py
class customerView(UpdateView):
template_name="customer.html"
model = customer
form_class = customerForm
def get_context_data(self, **kwargs):
context = super(customerView, self).get_context_data(**kwargs)
context['pk'] = 1
context['numberOfObjects'] = customer.objects.all().count()
return context
def get_object(self, queryset = None):
try:
obj = customer.objects.get(id = 1)
except:
obj = customer.objects.create(id = 1)
return obj
def form_valid(self, form):
form.save()
return HttpResponseRedirect(reverse("customer_view", kwargs={'pk': self.kwargs['pk']}))
这是 customer.html 的一些代码,用于查看customer
和building
<form action="" method="post">
{% csrf_token %}
{{ form.id }}
<div class="text_label" >FIRST NAME</div>
<div class="text_input">{{ form.FIRST_NAME }}</div>
<div class="text_label" >LAST NAME</div>
<div class="text_input">{{ form.LAST_NAME }}</div>
<div class="text_label" >BUILDING USE</div>
<div class="text_input3">{{ form.BUILDING_USE }}</div></td>
<div class="text_label" >BUILDING FLOORSPACE</div>
<div class="text_input3">{{ form.BUILDING_FLOORSPACE }}</div></td>
<div class="text_label">BUILDING YEAR</div>
<div class="text_input3" >{{ form.BUILDING_YEAR }}</div></td></tr>
rest of the code....//the first part is from the class customer and the second part is from the class building.
我的问题是我可以通用查看班级客户,但不能查看班级建设。我如何做到这一点...我仍然是 django 的业余爱好者,并且仍然是一个很好的方法。如果我问一个菜鸟问题,请原谅我。检查这个如何将两个模型传递给基于类的通用视图但仍然是空白的。非常感谢您的帮助。