无法加载资源:服务器在djang中的状态为405(不允许使用方法)

2024-03-29 06:06:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在django制作一个应用程序。这是我的索引.html第页。在

<!DOCTYPE html>

<html>
<head>
    <title>The index page</title>
</head>
<body>
    <h1>Choose the name of student</h1>
    <form action= "{% url 'detail' %}" method="post" enctype="multipart/form-data">
    {% csrf_token %}
            <select name="namedrop">
                {% for name in student_list %}
                <option>{{name.stuname}}</option>
                {% endfor %}

            </select>

    <input type="submit" name="submit">
    </form>
</body>
</html>

它创建了一个学生姓名的下拉列表。当我选择一个并单击submit按钮时,它将打开一个新的页面详细信息,但是在页面上没有显示任何内容。它显示的错误是加载资源失败:服务器的响应状态为405(不允许使用方法)。 我的视图.py具体如下:

^{pr2}$

这是网址.py在

from django.conf.urls import url
from . import views
from .models import student

urlpatterns= [
    url(r'^$',views.IndexView.as_view(),name='index'),
    url(r'^detail/$',views.DetailView.as_view(),name='detail'),

]


from .models import student
from django.views import generic
from django.views.generic import View
from django.http import Http404
from django.shortcuts import render



class IndexView(generic.ListView):
    template_name= 'studinfo/index.html'
    context_object_name= 'student_list'

    def get_queryset(self):
        return student.objects.all()

def detail(request,student_id):
    try:

        p = student.objects.get(pk=student_id)
    except student.DoesNotExist:

        raise Http404("Poll does not exist")
    return render(request, 'studinfo/detail.html', {'name': p})

现在这是我的视图..现在它引发了一个错误..TypeError位于/studinfo/detail/ detail()只接受2个参数(给定1个) 请求方式:POST 500(内部服务器错误)


Tags: djangonamefromimportformurlindexhtml
1条回答
网友
1楼 · 发布于 2024-03-29 06:06:54

DetailView无法处理post请求。DetailView只允许get请求。当您使用错误的请求方法时,会引发405方法不允许的错误,因为它不允许post-request-it-it引发405错误。在

我看到你有一个getname视图。我想你是想用这个。如果不是这样,则必须将url更改为接受post请求的视图。关于DetailView的更多信息可以阅读here

相关问题 更多 >