我创建了三个文件:
2- view.py:
class AddTeamView(View):
def get (self, request):
form = TeamForm()
context = {'form': form}
return render(request, 'add_team.html', context)
1-forms.py:
class TeamForm(forms.Form):
name = forms.CharField( max_length='100')
details = forms.CharField(max_length='250')
3-add_team.html:
-这里还有一个名为“ base.html”的文件
{% extends 'base.html' %}
{% block title %}
add team
{% endblock %}
{% block content %}
<form action="/add_team/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
然后我进入cmd并进入服务器“ python manage.py runserver”
它出现在浏览器中:
“此页面无法正常运行 如果问题仍然存在,请与网站所有者联系。 HTTP错误405“
答案 0 :(得分:0)
鉴于存在相应方法,视图可以支持using System.Runtime.Serialization;
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Microsoft.XLANGs.BaseTypes;
namespace Yournamespace.Components
{
public abstract class BaseFormatter : IFormatter
{
public virtual SerializationBinder Binder
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public virtual StreamingContext Context
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public virtual ISurrogateSelector SurrogateSelector
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public abstract void Serialize(Stream stm, object obj);
public abstract object Deserialize(Stream stm);
}
public class RawStringFormatter : BaseFormatter
{
public override void Serialize(Stream s, object o)
{
RawString rs = (RawString)o;
byte[] ba = rs.ToByteArray();
s.Write(ba, 0, ba.Length);
}
public override object Deserialize(Stream stm)
{
StreamReader sr = new StreamReader(stm, true);
string s = sr.ReadToEnd();
return new RawString(s);
}
}
[CustomFormatter(typeof(RawStringFormatter))]
[Serializable]
public class RawString
{
[XmlIgnore]
string _val;
public RawString(string s)
{
if (null == s)
throw new ArgumentNullException();
_val = s;
}
public RawString()
{
}
public byte[] ToByteArray()
{
return Encoding.UTF8.GetBytes(_val);
}
public override string ToString()
{
return _val;
}
}
}
,GET
,POST
等方法,因此视图应具有{{ 1}},PUT
,.get(..)
等功能。
此处您仅实现了.post(..)
,因此不允许.put(..)
请求。
根据您显示的数据,这看起来像CreateView
[Django-doc]的典型用例。这些视图的思想是封装常见方案,以便通过覆盖一些属性来创建针对特定情况量身定制的视图,例如:
def get(self, request)
POST
应该是class AddTeamView(CreateView):
form_class = TeamForm
template_name = 'add_team.html'
success_url = '/some/success_url'
,或者至少是TeamForm
,在这里您覆盖ModelForm
函数以将数据正确地保存到数据库,因为现在,则表单不会做任何事情(它会接收数据,但在验证后会将其丢弃)。
如果您不想要重定向到Form
,则可能需要覆盖.save(..)
函数。此外,从给定的视图名称中延迟解析form_valid(..)
是很常见的,例如:
success_url
答案 1 :(得分:0)
因此,我们无需执行条件检查请求是POST还是GET:
您的 views.py:
from django.views.generic import View
class AddTeamView(View):
def post(self, request):
form = TeamForm(request.POST)
if form.is_valid():
new_tm = TeamModel(name=form.cleaned_data['name'], details=form.cleaned_data['details'])
new_tm.save()
return redirect('team_list')
return render(request, 'add_team.html', {'form': form})
def get(self, request):
form = TeamForm()
return render(request, 'add_team.html', {'form': form})
希望这对您有帮助...