Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python html格式中不允许使用django HTTP 405方法_Python_Django_Http - Fatal编程技术网

Python html格式中不允许使用django HTTP 405方法

Python html格式中不允许使用django HTTP 405方法,python,django,http,Python,Django,Http,我是Django的新手,我一直在尝试开发一个简单的网站,询问用户的电子邮件地址和高度。然后,它将其保存在数据库中,并向用户发送电子邮件,并将用户重定向到一个页面,表示成功 现在的问题是,每当我按下“提交”键时,就会出现HTTP 405方法不允许的错误 # urls.py urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), #url(r'^success/$', views.SuccessView

我是Django的新手,我一直在尝试开发一个简单的网站,询问用户的电子邮件地址和高度。然后,它将其保存在数据库中,并向用户发送电子邮件,并将用户重定向到一个页面,表示成功

现在的问题是,每当我按下“提交”键时,就会出现HTTP 405方法不允许的错误

# urls.py
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    #url(r'^success/$', views.SuccessView.as_view(), name='success'),
]

#forms.py 班级高度表(forms.ModelForm):


# views.py
class IndexView(generic.ListView):
    form_class = HeightForm
    template_name = 'heights/index.html'

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

class HeightFormView(View):
    form_class = HeightForm
    template_name = 'heights/success.html'

    def get(self, request):
        form = form_class(None)

    def post(self, request):
        print('a' * 1000)
        form = form_class(request.POST)

        if form.is_valid:
            email = form.cleaned_data['email']
            height = form.cleaned_data['height']

            form.save()

            return HttpResponseRedirect(template_name)

    #render(request, template_name, {'form': form})
# index.html
{% extends 'heights/base.html' %}

{% block body %}
    <h1>Collecting Heights</h1>
    <h3>Please fill the entries to get population statistics on height</h3>
    <form action="" method="post">
        {% csrf_token %}
        <input type="email" name="email" placeholder="Enter your email address" required="true"/><br />
        <input type="number" min="50" max="300" name="height" placeholder="Enter your height in cm" required="true" /><br /><br />
        <input type="submit" name="submit" />
    </form>

    <a href="#">Click to view all heights in database</a>
{% endblock body %}

# views.py
class IndexView(generic.ListView):
    form_class = HeightForm
    template_name = 'heights/index.html'

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

class HeightFormView(View):
    form_class = HeightForm
    template_name = 'heights/success.html'

    def get(self, request):
        form = form_class(None)

    def post(self, request):
        print('a' * 1000)
        form = form_class(request.POST)

        if form.is_valid:
            email = form.cleaned_data['email']
            height = form.cleaned_data['height']

            form.save()

            return HttpResponseRedirect(template_name)

    #render(request, template_name, {'form': form})
# index.html
{% extends 'heights/base.html' %}

{% block body %}
    <h1>Collecting Heights</h1>
    <h3>Please fill the entries to get population statistics on height</h3>
    <form action="" method="post">
        {% csrf_token %}
        <input type="email" name="email" placeholder="Enter your email address" required="true"/><br />
        <input type="number" min="50" max="300" name="height" placeholder="Enter your height in cm" required="true" /><br /><br />
        <input type="submit" name="submit" />
    </form>

    <a href="#">Click to view all heights in database</a>
{% endblock body %}
#index.html
{%extends'heights/base.html%}
{%block body%}
收集高度
请填写这些条目以获取身高的人口统计数据
{%csrf_令牌%}



{%endblock body%}
代码甚至不能到达
print('a'*1000)
行而不产生错误。Chrome只是转到一个
此页面不工作
页面并显示
HTTP错误405

我用谷歌搜索了这个错误,但没有发现任何有用的东西。谢谢你的帮助


谢谢

您似乎没有为HeightFormView定义任何URL。表单由IndexView呈现,并返回到其自身;该视图不允许使用POST方法


您需要为HeightFormView定义一个URL,并通过
{%URL%}
标记在操作中引用它。

为您的表单添加一个在URL.py中提交的路由,并在操作中使用相同的路由。应该行得通

urlpatterns=[
url(r'^$',views.IndexView.as_view(),name='index'),
url(r“^saveForm/$”,views.HeightFormView.as_view(),name='form'),
]

在html格式中

<form action="/saveForm" method="post">


你的路线在
url.py
中的什么位置?表单的
action
中的路径在哪里?@Prakash Palnati更新了添加
url.py的代码。而且我不确定表单的
action=“
中应该放什么。应该是
{%url'heights:success%}
之类的吗?为表单添加一个在url.py中提交的路由,并在操作中使用相同的路由。应该行得通。