-1

I want to use Django as backed with react and I have already integrated Django with react app. And my react app(blog in my case) is up and running with Django. I have made following changes to do so.

Dir for templates inside settings.py:

enter image description here

Dir for Static Files inside settings.py: enter image description here

In urls.py rendered template without using views.py: enter image description here

But now I want to use the Django's db.sqlite3 as my database for react app. Please tell me how to configure it ? What additional settings I need to configure to fetch data from db.sqlite3 database inside react app ?

It will better if you can give an example code in which react app fetching and storing data to Django database by any method.

If more information about the settings are required then tell me I will edit my question accordingly.

  • 1
    There is no magical setting. You will have to write code. Or use an API framework (and write a little less code). – Klaus D. Nov 05 '20 at 06:30
  • 1
    There is no such thing as using the database on your server from a frontend framework such as React. That would defeat the purpose of MVC. You should read a lot on how to develop web applications. – Selcuk Nov 05 '20 at 06:33
  • read on & use "django rest framework" and how to integrate RESTful API in react – Tibebes. M Nov 05 '20 at 06:43
  • You don t need to do nothing. Write the models , write the views. (If you want to not complicate use rest api) and then from react you make the requests to get data – Florin Nov 05 '20 at 06:47
  • Can you please explain how to use that Django Rest API to do so ? – shivansh shrivastava Nov 05 '20 at 06:57
  • It will better if you can give an example code in which react app fetching and storing data to Django database by any method(Rest API or using Models). – shivansh shrivastava Nov 05 '20 at 07:11
  • @Selcuk I know that react cannot communicate with database as it is frontend. My question is suppose we have a form written in React JS and the on submit button I want that form information to store in db.sqlite3 database in Django. Then how can I do so ? – shivansh shrivastava Nov 05 '20 at 08:15
  • @Florin can you explain the same in detail with an example if it will work I will accept your answer then. – shivansh shrivastava Nov 05 '20 at 08:16
  • @shivanshshrivastava Sorry, the way the question was written hinted otherwise. All good then. – Selcuk Nov 05 '20 at 13:15

1 Answers1

5

Sqlite is your default database in Django. You can't directly access DB objects from React. You need to create REST API on Django side. The REST API is accessible on React side. You can send request from frontend to backend with it. Then Django based on request will have access to DB. It will process DB objects and return the results as a response.

I'm working on Django+React complete tutorial how to start SaaS from scratch. I hope it might help you: link to my tutorial

django react

pplonski
  • 5,023
  • 1
  • 30
  • 34