3

When I deploy my django project with

python manage.py runserver, and my react app with npm start,

I don't know which one is my "real project". I think the question boils down to client sided rendering vs server side rendering, and for now I want to go with server side rendering as I am more familiar with django than I am with react. That would mean I would have to set up urls and views with django/drf correct? If so, my questions are as follows...

  1. How do I "invoke" react whenever I go to a url (for example, website.com/items). I want to return a list of all my items in a nicely formatted html file. I suspect this has to do with a template parameter in the path() function.

  2. How do I prevent users from accessing api views (website.com/api/items), which is the url I use to handle api calls with django-rest-framework, but I don't want people to see this but also want react to "see" this.

  3. Because I am doing server side rendering, is there anything else my react app needs to do other than do the correct http calls to my api urls and making my html template look pretty?

Project URLS:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('App.urls')),
    path('api/',include('App.api.urls'))
]

App URLS:

urlpatterns = [
    path('', views.index, name='index'), #nothing much here yet
]

Api URLS:

urlpatterns = [
    path('item',views.ListItem.as_view())
]

My basic React App.js that just grabs all the items and displays them

class App extends Component {
    state = {
    items: []
    };
   async componentDidMount(){
    try{
    const res = await fetch('http://127.0.0.1:8000/api/item');
    const items = await res.json();
    this.setState({
        items});
    }
    catch (e){
        console.log(e);}
   }
    render(){
        return(
            <div>
                {this.state.items.map(item =>(
                    <div key={item.id}>
                        <h1>{item.item}</h1>
                        <h1>{item.price}</h1>
                         <span>{item.description}</span>
                    </div>
                ))}
             </div>
         );
        }

}
export default App;

1
  • If what you want is server-side react rendering, then I would not use Django at all, but rather node.js (and KOA/Express, etc.). This will allow you to take full advantage of the benefits of server-side rendering. While you can use both node.js and Django, you loose some of the benefits that server-side rendering gives you (mostly because using two server-side apps is likely to increase the latency of the rendering). Commented Mar 10, 2020 at 22:00

1 Answer 1

1

If you would like to server side render your react app, you have to set up your javascript inside a template view in your django app. Right now you are running your react app and django app in different servers, so this is why you are not getting server side render for react app. this would involve creating react with webpack or pacel.

What I would do is bundle the react app inside a django view, that way is server side render.

I hope this makes sense.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm I see. I think I saw an article that mentioned webpacks. Thank you! As for the other parts of my question, would you happen to know how to "hide" the api urls so someone doesn't randomly access it?
As far as "hiding" api urls, you can't really do that. See more details here: stackoverflow.com/a/29905352/8031815 (the question is about web sockets, but the answer applies to regular http connections as well).
In this case Django is doing SSR and it is not server side rendering your React app but some other template (maybe the shell around your React app). The way I know it is that if you want React SSR, you'd run JS on the backend to well... render your React app, which is a JS app, on the backend (hence SSR). After loading the SSRed page in the browser, you'd then ReactDOM.hydrate that page i.e. as opposed to SPA, were you would ReactDOM.render, you would "attach" to the already SSRed page (and attach even listeners etc.. to it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.