【问题标题】:Django UpdateView Method Not Allowed (POST)Django UpdateView 方法不允许(POST)
【发布时间】:2021-03-19 21:34:34
【问题描述】:

当我在更新页面 (http://127.0.0.1:8000/account/15/update) 上单击“保存”时,我得到了

[19/Mar/2021 13:47:18] "GET /account/15/update HTTP/1.1" 200 2311
Method Not Allowed (POST): /account/15/
Method Not Allowed: /account/15/
[19/Mar/2021 13:47:24] "POST /account/15/ HTTP/1.1" 405 0

数据库记录没有得到更新,所以看起来有些事情默默地失败了。查看错误消息,我们似乎最终尝试在“帐户详细信息”页面而不是“帐户更新”页面上进行 POST - 这是怎么发生的?

这是我的帐户\urls.py

from django.urls import path
from .views import (
AccountCreateView,
AccountListView,
AccountDetailView,
AccountUpdateView,
AccountDeleteView
)

app_name = 'account'
urlpatterns = [
path('', AccountListView.as_view(), name='account-list'),
path('create/', AccountCreateView.as_view(), name='account-create'),
path('<int:pk>/', AccountDetailView.as_view(), name='account-detail'),
path('<int:pk>/update', AccountUpdateView.as_view(), name='account-update'),
path('<int:pk>/delete', AccountDeleteView.as_view(), name='account-delete'),
]

这是我的 Accounts\views.py

from django.shortcuts import render, get_object_or_404, redirect

from django.views.generic import (
    CreateView,
    DetailView,
    ListView,
    UpdateView,
    DeleteView
)

from .forms import AccountForm
from .models import Account

class AccountCreateView(CreateView):
    form_class = AccountForm
    template_name = 'Accounts/account_create.html'
    queryset = Account.objects.all().order_by("-pk")

    def form_valid (self, form):
        return super().form_valid(form)

class AccountDetailView(DetailView):

    def get_object(self):
        account_id = self.kwargs.get("pk")
        return get_object_or_404(Account, account_id=account_id)

class AccountUpdateView(UpdateView):
    form_class = AccountForm
    template_name = 'Accounts/account_create.html'

    def get_object(self):
        account_id = self.kwargs.get("pk")
        return get_object_or_404(Account, account_id=account_id)

    def form_valid (self, form):
        return super().form_valid(form)

这是我的 Accounts\account_create.html(我将其用于创建和更新)

{% extends 'base.html' %}
{% block content %}
    <h1>Account Create/Update Page for {{ request.user }} </h1>
    This is the InApp Account Create/Update Page<p/>
    <form action="." method='POST' enctype="multipart/form-data"> {% csrf_token %}
        <table id="accountdetail">
            {{ form.as_table }}
        </table>
        <p/>
        <input type="submit" value="Save"/> &nbsp;&nbsp;
        <button onclick="location.href='/account'" type="button">Cancel</button>
    </form>
{% endblock %}

我错过了什么?谢谢。

【问题讨论】:

    标签: python-3.x django-views django-3.1


    【解决方案1】:

    HTML 中的表单明确将 POST 数据发送到“.”:

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

    所以我相信/account/15/update 会变成/account/15,就像向上移动一个目录一样。如果您真的想使用相同的模板,您可以尝试以下方法:

    {% if "update" in request.get_full_path %}
     <form action="{% url 'account-update' account.id %}" method="PUT">
    {% else %}
     <form action="{% url 'account-create' account.id %}" method="POST" enctype="multipart/form-data">
    

    另外,请注意 enctype 属性只能与 method="POST" 一起使用

    【讨论】:

    • 感谢您的帮助。你为我指明了正确的方向。 Django UpdateView 确实支持 POST,我只需要删除 'action="."'。
    • 太棒了,不客气!我实际上并不知道更新视图也支持 POST,我通常使用功能视图进行控件,但现在尝试基于类的视图,它们很棒
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2021-04-05
    • 2018-05-30
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多