我在django中製作應用程序。這是我的index.html頁面。加載資源失敗:服務器響應狀態爲405(方法不允許)在django
<!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>
它創建了一個學生姓名下拉列表。當我選擇一個並單擊提交按鈕時,它會打開一個新的頁面詳細信息,但無法在頁面上顯示任何內容。它顯示的錯誤是無法加載資源:服務器響應狀態爲405(METHOD NOT ALLOWED)。 我views.py是如下:
from .models import student,subject
from django.views import generic
from django.views.generic import View
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()
class DetailView(generic.DetailView):
model= student,subject
template_name='studinfo/detail.html'
def getname(request):
if request.method=='POST':
name=request.get['namedrop']
return render(request, 'detail.html', {'name':name})
這是urls.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})
這是我的視圖now..now它引發錯誤..TypeError在/ studinfo /細節/ 細節()恰恰2個參數(1給出) 請求方法:POST 500(內部服務器錯誤)
'請儘快幫忙'。不要試圖將這些短語納入您的問題。我編輯了問題以刪除該部分 –
在問題中包含您的urls.py。包含您收到的錯誤的完整回溯 –
顯示您的'urls.py'。你是否使用'getname'?如果您使用'DetailView',則無法處理POST請求。從django.conf.urls中導入 –