Reputation: 251
i am making an app in django. this is my index.html page.
<!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>
it creates a drop down list of the name of students. when i select one and click on submit button, it opens a new page detail but fails to show anything on the page. the error it is showing is Failed to load resource: the server responded with a status of 405 (METHOD NOT ALLOWED). my views.py is as below:
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})
this is 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})
this is my view now..now it is raising an error ..TypeError at /studinfo/detail/ detail() takes exactly 2 arguments (1 given) Request Method: POST 500 (INTERNAL SERVER ERROR)
Upvotes: 2
Views: 1395
Reputation: 9931
DetailView
can not process post requests. DetailView
only allows get requests. 405 method not allowed error is raised when you are using a wrong request method since it does not allow post request it it raising 405 error.
I see that you have a getname
view. I think you were trying to use that. If not that then you have to change your url
to a view that accepts post requests. More on DetailView
can be read here
Upvotes: 1