私は django フレームワーク開発者には不慣れで、クラスベースのビューとフォームのドキュメントをたくさん読んできました。ここで、車のリストと新しい車を作成するためのフォームを含む単一のページを (テスト目的で) 作成したいと考えています。
これは私のviews.pyです
class IndexView(ListView):
template_name = "index.html"
context_object_name = "cars"
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context["form"] = CarForm
return context
def get_queryset(self):
self.brand = self.kwargs.pop("brand","")
if self.brand != "":
return Car.objects.filter(brand__iexact = self.brand)
else:
return Car.objects.all()
def post(self, request):
newCar = CarForm(request.POST)
if newCar.is_valid():
newCar.save()
return HttpResponseRedirect("")
else:
return render(request, "index.html", {"form": newCar})
class CarForm(ModelForm):
class Meta:
model = Car
delete = True
これは私が作成したいものの写真です。
私の質問は次のとおりです。
1)これはこの目的のための「ベストプラクティス」ですか?2) テンプレートの {{ car.name.errors }} は常に空白です (検証エラーは表示されません)。
ありがとう!…そして私の英語でごめんなさい。