如何重定向到React/Django中的新博客文章
我正在使用React/Django建立博客网站。这是我的基本新博客组件:
import React, {useState} from 'react';
import { Link } from 'react-router-dom';
function NewBlog(props) {
const [title, setTitle] = useState('')
const [body, setBody] = useState('')
function postBlog() {
fetch('http://localhost:8000/api/blogs/', {
"headers": {
"content-type": "application/json",
},
"body": JSON.stringify({
"title": title,
"author": 1,
"body": body
}),
"method":"POST",
})
}
return (
<div>
<form onSubmit={postBlog}>
<textarea className='title' placeholder='title...' onChange={(ev) => setTitle(ev.target.value)} value={title}></textarea>
<textarea className='body' placeholder='' onChange={(ev) => setBody(ev.target.value)} value={body}></textarea>
<button>POST</button>
</form>
<div>
<Link to='/blog'>BACK</Link>
</div>
</div>
);
}
export default NewBlog;
这可以发布一个新博客,但是在提交表格后,我不知道如何重定向到该特定博客,这只是http:// localhost:8000/博客/blog_id
。取而代之的是,该表单才被清除,我必须转到&lt; link = ='/blog'&gt; back&lt;/link&gt;
to Bloglist才能查看新的博客。
现在,我知道使用Django,您可以使用他们的表格发布博客,并且一旦发布,它将重定向到该blog_detail
。我的问题是我不知道该如何使用前端的反应。我可以使用某种重定向或路线吗?另外,我不确定我什至如何检索刚刚寄出的博客的ID。
抱歉,如果这不是很多事情要做,我不知道我可以显示的其他代码中的其他内容,这将有助于这个问题,因为逻辑可以发表博客,我只是无法了解该特定的特定内容帖子后博客。
I'm building a blog site with React/Django. Here's my basic NewBlog component:
import React, {useState} from 'react';
import { Link } from 'react-router-dom';
function NewBlog(props) {
const [title, setTitle] = useState('')
const [body, setBody] = useState('')
function postBlog() {
fetch('http://localhost:8000/api/blogs/', {
"headers": {
"content-type": "application/json",
},
"body": JSON.stringify({
"title": title,
"author": 1,
"body": body
}),
"method":"POST",
})
}
return (
<div>
<form onSubmit={postBlog}>
<textarea className='title' placeholder='title...' onChange={(ev) => setTitle(ev.target.value)} value={title}></textarea>
<textarea className='body' placeholder='' onChange={(ev) => setBody(ev.target.value)} value={body}></textarea>
<button>POST</button>
</form>
<div>
<Link to='/blog'>BACK</Link>
</div>
</div>
);
}
export default NewBlog;
This works to POST a new blog, but after I submit the form, I can't figure out how to redirect to that specific blog, which would just be http://localhost:8000/blogs/BLOG_ID
. Instead, the form is just cleared, and I have to go <Link to='/blog'>BACK</Link>
to the BlogList to see the new blog rendered.
Now, I know with Django you can use their form to post a blog, and, once posted, it redirects to that blog_detail
. My issue is I don't know how to do that using React on the front end. Is there some kind of Redirect or Route I can use? Also, I'm not sure how I'd even retrieve that just-posted blog's id either.
Sorry if this isn't a lot to go on, I don't know what other piece of my code I could show that would help the question seeing as the logic works to post a blog, I just can't get to that specific blog after post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是使Post请求输出刚刚使用其ID创建的新博客对象。
因此,您可以只使用React-Router-dom的
usenavigate
挂钩,然后转到该页面One solution would be to make the post request output the new blog object that just got created with its id.
so then you could just use the
useNavigate
hook from react-router-dom and go to that page