decoupled frontend and backend with Django, webpack, reactjs, react-router

2024/10/9 14:24:38

I am trying to decouple my frontend and my backend in my project. My frontend is made up of reactjs and routing will be done with react-router, My backend if made form Django and I plan to use the front end to make API (ajax) calls to Django.

Right now I'm not sure how to get these two ends to talk to each other correctly.

Here is the link to my project

Here is my project structure:

/cherngloong/app (frontend)/cherngloong/templatesindex.jtmlurls.pysettings.py.../contacturls.pyviews.py

I use webpack to build all my JS and CSS and place it into index.html with webpack_loader which looks like this:

{% load render_bundle from webpack_loader %}
<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>Example</title></head><body><div id="app"></div>{% render_bundle 'main' %}</body>
</html>

In Django here are my cherngloong/urls.py:

urlpatterns = [url(r'^admin/', include(admin.site.urls)),url(r'', TemplateView.as_view(template_name='index.html')),url(r'^api/', include('contact.urls'))
]urlpatterns += staticfiles_urlpatterns()

I don't want to serve my app from django or make django to serve the same view on ANY url.

Here are my react-router routes:

var routes = (<Router><Route path="/" component={ Views.Layout } ><Route path="contact"  component={ Views.Contact }/></Route><Route path="*" component={ Views.RouteNotFound } /></Router>
);export default routes;

I can currently run the server but when I run the front end portion, I see this in the developer tools

http://localhost:8000/static/public/js/main.js Failed to load resource: the server responded with a status of 404 (NOT FOUND)
Answer

Your settings.py defines the following:

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'app'),
)

This will serve the files in /app/ directory. so the url /static/public/js/ translates to the /app/public/js/ directory.

What you want is to serve the files in the /public/ dir. Use the following settings:

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'public'),
)WEBPACK_LOADER = {'DEFAULT': {'BUNDLE_DIR_NAME': 'js/','STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json')}
}

Also, the urls are different in the code you posted here and in github. The url(r'', Templa... will match ALL urls and just render index.html even when making calls to /api/. Move this line to the bottom:

urlpatterns = [url(r'^admin/', include(admin.site.urls)),url(r'^api/', include('contact.urls')),
]urlpatterns += staticfiles_urlpatterns()urlpatterns += [url(r'', TemplateView.as_view(template_name='index.html')),
]
https://en.xdnf.cn/q/70006.html

Related Q&A

Map colors in image to closest member of a list of colors, in Python

I have a list of 19 colors, which is a numpy array of size (19,3):colors = np.array([[0, 0, 0], [0, 0, 255], [255, 0, 0], [150, 30, 150], [255, 65, 255], [150, 80, 0], [170, 120, 65], [125, 125,…

Storing a file in the clipboard in python

Is there a way to use the win32clipboard module to store a reference to a file in the windows clipboard in python. My goal is to paste an image in a way that allows transparency. If I drag and drop a…

retrieve intermediate features from a pipeline in Scikit (Python)

I am using a pipeline very similar to the one given in this example : >>> text_clf = Pipeline([(vect, CountVectorizer()), ... (tfidf, TfidfTransformer()), ... …

Any way to do integer division in sympy?

I have a very long expression that I think can be simplified, and I thought sympy would be the perfect way to do it. Unfortunately the formula relies on a couple of integer divides, and I cant find any…

Scrapy LinkExtractor - Limit the number of pages crawled per URL

I am trying to limit the number of crawled pages per URL in a CrawlSpider in Scrapy. I have a list of start_urls and I want to set a limit on the numbers pages are being crawled in each URL. Once the l…

Python Invalid format string [duplicate]

This question already has answers here:Python time formatting different in Windows(3 answers)Closed 9 years ago.I am trying to print the date in the following format using strftime: 06-03-2007 05:40PMI…

Python template safe substitution with the custom double-braces format

I am trying to substitute variables in the format {{var}} with Pythons Template. from string import Templateclass CustomTemplate(Template):delimiter = {{pattern = r\{\{(?:(?P<escaped>\{\{)|(?P…

Emit signal in standard python thread

I have a threaded application where I do have a network thread. The UI-part passes a callback to this thread. The thread is a normal python thread - its NO QThread.Is it possible to emit PyQT Slot with…

Sqlalchemy from_statement() cannot locate column

I am following the sqlalchemy tutorial in http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.htmlNevertheless, instead of using a SQLite backend, I am using MySQL. The problem is that when I try to exe…

Python - how to check if weak reference is still available

I am passing some weakrefs from Python into C++ class, but C++ destructors are actively trying to access the ref when the real object is already dead, obviously it crashes...Is there any Python C/API a…