【发布时间】:2021-02-13 01:17:04
【问题描述】:
您好,我刚开始学习 react 并决定使用 ant design(第 3 版),所以我创建了一个 API(Django Rest 框架)并运行它,我的登录和注册页面运行良好,所以我决定将 ANT Design 升级到版本 4,我不得不阅读文档,因为发生了一些变化并设法让它看起来正常,但是现在当我填写登录表单提交时,我得到“请求失败,状态码 400”......然后检查网络,我看到一个响应:{“密码”:[“此字段可能不是空白。”]}
我尝试从 API 登录,它运行良好,但在我尝试使用表单时一直显示 Request failed with 404。
这是 Form.Js
class LoginForm extends React.Component {
state = {
username: "",
password: ""
};
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
onFinish = values => {
console.log(values);
// values.preventDefault();
const { username, password } = this.state;
this.props.login(username, password);
};
render() {
const { error, loading, token } = this.props;
const { username, password } = this.state;
if (token) {
return <Redirect to="/" />;
}
return (
<Layout>
<Layout>
<Layout style={{ padding: '0 24px 24px', background: '#fff' }}>
<Tabs defaultActiveKey="1" onChange={callback}>
<TabPane tab="Login" key="1">
<Content
style={{
background: '#fff',
padding: 24,
margin: 0,
minHeight: 280,
}}
>
{/* {this.props.children} */}
<h2>Log in to your account</h2>
<div>
{error && <p>{this.props.error.message}</p>}
<React.Fragment>
<Form
{...layout}
name="basic"
initialValues={{ remember: false }}
onFinish={this.onFinish}
onFinishFailed={onFinishFailed}
// onSubmit={this.handleSubmit}
>
<Form.Item
onChange={this.handleChange}
value={username}
label="Username"
name="username"
rules={[{ required: true, message: 'Please input your username!' }]}
>
<Input />
</Form.Item>
<Form.Item
onChange={this.handleChange}
value={password}
label="Password"
name="password"
rules={[{ required: true, message: 'Please input your password!' }]}
>
<Input.Password />
</Form.Item>
<Form.Item {...tailLayout} name="remember" valuePropName="checked">
<Checkbox>Remember me</Checkbox>
</Form.Item>
<Form.Item {...tailLayout}>
<Button type="primary"
loading={loading}
disabled={loading}
htmlType="submit">
Log in
</Button>
</Form.Item>
</Form>
</React.Fragment>
</div>
</Content>
</TabPane>
<TabPane tab="Sign Up" key="2">
Content of Tab Pane 2
</TabPane>
</Tabs>
</Layout>
</Layout>
</Layout>
);
}
}
const mapStateToProps = state => {
return {
loading: state.auth.loading,
error: state.auth.error,
token: state.auth.token
};
};
const mapDispatchToProps = dispatch => {
return {
login: (username, password) => dispatch(authLogin(username, password))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(LoginForm);
【问题讨论】:
标签: reactjs django django-rest-framework antd django-react