不允许在django中发布方法 [英] Method not allowed Post in django

查看:97
本文介绍了不允许在django中发布方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在我的应用程序中添加post方法时,它会显示此消息:

when i try to add a post method in my app it shows this message :

不允许使用方法(发布):HTTP/1.1 405 0

Method not allowed (Post): HTTP/1.1 405 0

Views.py:

class AddTeamView(View):
def get(self, request):
    form = TeamForm()
    context = {'form': form}
    return render(request, 'add_team.html', context)

add_team.html:

add_team.html :

{% extends 'base.html' %}
{% block title %}
Add a Team
{% endblock %}
{% block content %}
<form action="/add_team/" method="post">
{% csrf_token %}
<!-- this form content is called from the view.py/context-->
{{ form }}
<input type="submit" value="اضافة "/>
</form>
{% endblock %}

urls.py:

urlpatterns =[
url(r'^admin/', admin.site.urls),
url(r'add_team/$', AddTeamView.as_view(), name='add-team-view'),
]

settings.py:

settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

forms.py:

from django import forms


class TeamForm(forms.Form):
name = forms.CharField(label='اسم الفريق')
details = forms.CharField(label='تفاصيل الفريق')

有人可以帮忙吗?

推荐答案

就像Daniel Roseman的评论所说,您需要在视图中添加一个post方法.提交填写的表单时,来自浏览器的HTTP请求是POST,而不是GET.

Like Daniel Roseman's comment says, you need to add a post method to your view. When you submit the filled form the HTTP request from your browser is a POST, not a GET.

查看 Django文档有关如何组织基本类视图的示例,例如您尝试与post和get方法一起使用的视图.

Check out the the Django documentation for an example of how to organize a basic class view like you are trying to use with a post and get method.

以下是针对您的案例修改的文档示例:

Here is the documentation example modified for your case:

class AddTeamView(View):
    form_class = TeamForm
    template_name = 'add_team.html'

    # Handle GET HTTP requests
    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form': form})

    # Handle POST GTTP requests
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            # <process form cleaned data>
            return HttpResponseRedirect('/success/')

        return render(request, self.template_name, {'form': form})

这篇关于不允许在django中发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆