React BrowserRouter路由不会更改网址

时间:2019-09-25 14:06:15

标签: django reactjs react-router django-urls

我正在尝试让我的React Router更改我的实际网址。现在它会更改组件,但不会更改浏览器中的实际网址

这是我的app.js

br: str = "check"

我正在尝试将 PaintingDetail 组件指向 / detailed-view /:slug 路径,但我的实际浏览器始终显示 http://127.0.0.1:8000/paintings/sad-man (在这种情况下,伤心人是the子)

我什至试图一起删除这行

import React, { Component } from 'react';
import './App.css';
import { BrowserRouter, Route, Redirect, Switch} from 'react-router-dom';

import PaintingList from './paintings/PaintingList';
import PaintingDetail from './paintings/PaintingDetail';
import PaintingCreate from './paintings/PaintingCreate';

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <Switch>   
        <Route exact path='/' component={PaintingList}/>
        <Route path='/paintings' component={PaintingList}/>
        <Route path='/paintings/create' component={PaintingCreate}/>
        <Route path='/detailed-view/:slug' component={PaintingDetail}/>
        </Switch>
      </BrowserRouter>
    );
  }
}

export default App;

该组件仍然有效,但浏览器仍显示 http://127.0.0.1:8000/paintings/sad-man

我正在使用Django作为后端,因此不确定是否与此有关。

这是我的绘画模型的url.py

<Route path='/detailed-view/:slug' component={PaintingDetail}/>

这是我的Django主文件夹中的urls.py:

from django.urls import path, re_path

from .views import (
        PaintingDetailAPIView,
        PaintingListCreateAPIView,
    )

app_name = 'paintings-api'

urlpatterns = [
    path('', PaintingListCreateAPIView.as_view(), name='list-create'),
    re_path(r'^(?P<slug>[\w-]+)/$', PaintingDetailAPIView.as_view(), name='detail'),
 ]

非常感谢!

1 个答案:

答案 0 :(得分:0)

实际上对不起大家这个愚蠢的问题。

问题是我需要更改链接到相同URL的组件以使其起作用。这是我的PaintingInline组件

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

import '../css/Paintinginline.css'

// react-router-dom <Link> documentation: https://reacttraining.com/react-router/web/api/Link

class PaintingInline extends Component {

    render() { 

        const {paintingItem} = this.props
        // console.log(paintingItem)
        // console.log(paintingItem.srcs[0].src)
        return ( 
            <div>
                {paintingItem !== undefined ? 
                <div className={"inline-container"}>     
                    <Link maintainScrollPosition={false} to={{
                        pathname:`/paintings/detail/${paintingItem.slug}`,
                        state:{fromDashboard: false}
                    }}>
                        <img    className={"inline-main-image"}
                                src={paintingItem.srcs[0].src}
                                /> 
                    </Link>
                    <h6>{paintingItem.title}</h6>
                    <a>{paintingItem.size_measurements}</a>   
                    <a>{paintingItem.artist}</a>
                    <a>${parseInt(paintingItem.price).toLocaleString()}</a>

                 </div>
                : ""}
            </div>

         );
    }
}

export default PaintingInline;

与app.js中的匹配所需的网址