2015-10-19 82 views
4

我試圖用我的Django應用程序使用reactjsreact-router1.x),但我很難將所有這些放在一起。這是github項目,只是在這個問題中我沒有提供足夠的信息。404頁未找到使用Django + react-router

https://github.com/liondancer/django-cherngloong

我在我的routes.js

var routes = (
    <Router> 
     <Route path="/" component={ Views.Layout }> 
      <IndexRoute component={ Views.Index } /> 
      <Route path="about" component={ Views.About } /> 
     </Route> 
     <Route path="*" component={ Views.RouteNotFound } /> 
    </Router> 
); 

export default routes; 

創建path="about"layout.js

class Layout extends React.Component { 
    constructor(props) { 
     super(props); 
    } 
    render() { 
     return (
      <div id="review-web"> 
       <header className="header"> 
        <LogoElement /> 
        <CenterPiece /> 
       </header> 
       <div> 
        { React.cloneElement(this.props.children, { path: this.props.path }) } 
       </div> 
       <Footer /> 
      </div> 
     ); 
    } 
} 

export default Layout; 

當我localhost.8000/about進入我得到一個404 Django的錯誤

enter image description here

我的目標是保持前端和後端分離,所以我相信我應該能夠使用Django作爲數據端點而不是渲染視圖。

+0

當你去「/」它成功地服務Views.Layout頁面嗎?對我來說,它看起來像你的應用程序不知道服務器或查看routes.js。你配置你的應用程序使用routes.js? – user2719875

+0

@ user2719875現在我的應用程序使用[this](https://github.com/liondancer/django-cherngloong/blob/master/assets/js/index.js)來使用'route.js'。如果我要輸入'localhost:8000 /',它會顯示[this](https://github.com/liondancer/django-cherngloong/blob/master/assets/js/views/index.js)組件] – Liondancer

+0

它可以工作,如果你這樣做:(參見我在路徑開頭使用的斜槓)。編輯:另外,你確定你不想在它自己包含「」這一行嗎?它目前嵌套在其他路線內。 – user2719875

回答

1

我的應用程序爲所有有效路線提供相同的客戶端JS。請原諒Flask代碼。

@blueprint.route('/stuff/<path:path>', methods=["get"]) 
@blueprint.route('/', methods=["get"]) 
def index(path=None): 
    return render_template('app.html') 
4

我面臨同樣的問題,並以此解決方案結束。

settings.py:

REACT_ROUTES = [ 
    'login', 
    'signup', 
    'password-reset', 
    'password-change', 
    'about', 
    ... 
] 

urls.py:

routes = getattr(settings, 'REACT_ROUTES', []) 
urlpatterns = [ 
    ... 
    url(r'^(%s)?$' % '|'.join(routes), TemplateView.as_view(template_name='index.html')), 
] 

我不喜歡重複Django中設置的所有反應,路線,但是如果我們把一切有url(r'^.*$'),服務器將始終返回HTTP status_code 200.這只是一種能夠爲不存在的url返回有效的HTTP status_code 404的方法。