代码之家  ›  专栏  ›  技术社区  ›  Arundeep Chohan

Django从表单上载重定向

  •  0
  • Arundeep Chohan  · 技术社区  · 4 年前

    从页面

    This page isn’t working. If the problem continues, contact the site owner.
    HTTP ERROR 405
    

    从终点站

    Method Not Allowed (POST): /
    Method Not Allowed: /
    [20/Dec/2021 22:00:27] "POST / HTTP/1.1" 405 0
    

    类型html->包含在侧边栏中。html->包括在家里。html

    <form method = "POST" action='.' enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Upload</button>
    </form>
    

    意见。派克

    from django.shortcuts import render
    from .forms import UserProfileForm
    
    def index(request):
        print(request.POST)
        return render(request,'home.html')
    

    网址。派克

    from django.conf import settings
    from django.urls import path
    from django.views.generic.base import TemplateView # new
    
    urlpatterns = [
        path('', TemplateView.as_view(template_name='home.html'), name='home'), 
    ]
    
    2 回复  |  直到 4 年前
        1
  •  1
  •   xStyl27    4 年前

    在你的网址。派克

    改为:

    路径(“”,索引,名称='home'),

    您还必须导入URL中的视图。派克

        2
  •  1
  •   cshelly    4 年前

    但是,当页面作为GET请求的响应时,POST属性中不应该包含空字典。 据我说

    def index(request):
        if request.method == "POST" :
            print(request.POST)
        return render(request,'home.html')
    
    

    根据Django文件

    供进一步参考- https://docs.djangoproject.com/en/3.2/ref/request-response/