django def post方法在cbv中不起作用

时间:2018-08-11 18:14:20

标签: django post django-forms

我不知道为什么我不能覆盖帖子。当我发布表格时,我进入“ /”目录,什么也没有发布。我已经知道表格可以正常工作,因为我可以将其用作fbv

我的观点

class ProfileUpdateView(LoginRequiredMixin, View):
    template_name = "accounts/update.html"

    def get_object(self):
        user = get_object_or_404(User, username=self.kwargs.get("username"))
        return user

    def get(self, request, *args, **kwargs):
        user = get_object_or_404(User, username=self.kwargs.get("username"))
        user_form = UserForm(instance=user)
        if user.is_client:
            print("client get is working")
            profile = Client.objects.get(id=user.clients.id)
            profile_form = ClientForm(instance=profile)
        if user.is_artisan:
            profile = Artisan.objects.get(id=user.artisans.id)
            profile_form = ArtisanForm(instance=profile)
        return render(
            request,
            self.template_name,
            {"user_form": user_form, "profile_form": profile_form},
        )

    def post(self, request, *args, **kwargs):
        print("post is working")
        if user.is_client:
            print("client post is working")
            profile_form = ClientForm(
                request.POST, request.FILES, instance=profile
            )
            user_form = UserForm(request.POST, request.FILES, instance=user)

        if user.is_artisan:
            profile_form = ArtisanForm(
                request.POST, request.FILES, instance=profile
            )
            user_form = UserForm(request.POST, request.FILES, instance=user)

        if profile_form.is_valid() and user_form.is_valid():
            print("form validation is working")
            created_profile = profile_form.save(commit=False)
            user_form.save()
            created_profile.save()
            reverse("accounts:profile", kwargs={"username": self.user.username})
        return render(
            request,
            self.template_name,
            {"user_form": user_form, "profile_form": profile_form},
        )

模板

<form action="." method="POST" enctype="multipart/form-data">
                  {% csrf_token %}
              {{ user_form.as_p }}
                <p> client form</p>
              ------------------------------------------------
                {{ profile_form.as_p }}
          <button class="btn btn-primary btn-round" type="submit">update</button>
    </form>

1 个答案:

答案 0 :(得分:0)

您的表单操作已设置为".",即“此目录”。

取而代之的只是将其发布到当前URL。

<form method="POST" enctype="multipart/form-data">