如何重定向到React/Django中的新博客文章

发布于 2025-01-31 02:12:13 字数 1642 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2025-02-07 02:12:13

一种解决方案是使Post请求输出刚刚使用其ID创建的新博客对象。

{
    id: #,
    ...

}

因此,您可以只使用React-Router-dom的usenavigate挂钩,然后转到该页面

import React, {useState} from 'react';
import { Link, useNavigate } from 'react-router-dom';

function NewBlog(props) {
    const [title, setTitle] = useState('')
    const [body, setBody] = useState('')
    const navigate = useNavigate()
    
    function postBlog() {
        fetch('http://localhost:8000/api/blogs/', {
            "headers": {
                "content-type": "application/json",
            },
            "body": JSON.stringify({
                "title": title,
                "author": 1,
                "body": body
            }),
            "method":"POST",
        })
        .then(response => response.json())
        .then(data => {
          navigate(`/blog/${data.id}`) 
        })
    }

    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;

One solution would be to make the post request output the new blog object that just got created with its id.

{
    id: #,
    ...

}

so then you could just use the useNavigate hook from react-router-dom and go to that page

import React, {useState} from 'react';
import { Link, useNavigate } from 'react-router-dom';

function NewBlog(props) {
    const [title, setTitle] = useState('')
    const [body, setBody] = useState('')
    const navigate = useNavigate()
    
    function postBlog() {
        fetch('http://localhost:8000/api/blogs/', {
            "headers": {
                "content-type": "application/json",
            },
            "body": JSON.stringify({
                "title": title,
                "author": 1,
                "body": body
            }),
            "method":"POST",
        })
        .then(response => response.json())
        .then(data => {
          navigate(`/blog/${data.id}`) 
        })
    }

    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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文