connecting multiple create-react-apps with django - python

I have one big Django project consisting out of several small apps, independent from each other but partly sharing the same database or/and models.
All the apps are created with python manage.py startapp myappa so they have their own urls.py and views.py.
I started using create-react-app for the front end for two of those apps and connected them successfully by turning the index.html of the react project into a template and serve it through the views.py (more here).
Now each one of these create-react-app projects has its own build folder which's path I include in the STATICFILES_DIRS in settings.py to collect the static files.
It looks like this:
static/
templates/
myappa/
build/
static/
index.html
...
myappb/
build/
static/
index.html
...
Now, I was wondering if there is a way to have only one build folder that contains all built files respectively to their projects something like:
myappa/
myappb/
static/
templates/
build/
myappa/
static/
index.html
...
myappb/
static/
index.html
...
I'm imagining that this folder would live on the same level as the the static folder and the appa and appb folders.
One way, since by default I can't have the build folder outside the project folder structure(?) and I don't want to eject the project, I manually move all the build folders a couple of levels up so that it is on the same level as the app folders after the build process. That way I only have to include one path into to STATICFILES_DIRS. But I'm wondering if there is an automatic way of doing this.
I've been looking at other projects of django and React but unfortunately, most are rather small so they don't cover this case.
I was wondering if anyone had the same thoughts or maybe some nice big projects on github I can have a look at? Or am I wrong and by default having its own create react app in the separated apps makes sense?

Related

Flask-Admin: How to use a different templates directory than the default?

My application is a backend REST API. I would like to add Flask-Admin, but only for use in nonprod.
I would like to keep the Flask admin related stuff separate from the rest of my application, in its own directory.
However, by default Flask Admin is using the same templates directory that my application would use.
How can I instruct Flask Admin to use a non-standard templates directory?
My app structure currently looks like this. In order to customize Flask-Admin, I need to create a templates/admin/ inside of my main api directory, which I don't want:
myapp/
myapp/
api/
app.py # Flask(__name__) defined here
views.py
templates/
admin/
index.html
admin/
myadmin.py
I instead want the Flask-Admin related things like the templates/admin/ directory under the admin directory, like this:
myapp/
myapp/
api/
app.py # Flask(__name__) defined here
views.py
admin/
myadmin.py
templates/
admin/
index.html
Is it possible to have Flask Admin use a non-standard templates directory?

how to make django serve non static js files?

My python backend server serves a react application as static files.
In react, everything inside the src directory is bundled when we build the app, however, there is one javascript file outside the src, in the public directory, that I left there because I want to be untouched after the build. the reason I did this, is because inside the src code, I create Workers, and to create workers we have to specify the url of the worker file. This file is called worker.js, it remains untouched after the build, but after deploying the backend, the frontend cannot access the /worker.js url because apparently django only serves static files, so /worker.js is not recognized and the index.html is returned instead. How can I make it possible to access this non-static js file?
Explanation of how static files work:
Development
Let's get this to work in a development situation first (django is serving your static files using runserver). STATIC_ROOT is not relevant in this case.
Django can find all your static files from these locations:
Any /static/ folder located inside your apps, e.g. if you have a blog app, then the folder /blog/static/ will be parsed.
Any folder specified in STATICFILES_DIRS, so for example if you have an assets folder in your main project, you'd add it to STATICFILES_DIRS with os.path.join(BASE_DIR, "assets") where BASE_DIR is the location of manage.py.
What if both the blog app and your assets directory need a "logo.png"? Instead of worrying that images, js files, css files have unique names, use namespacing. Inside the blog app, put all static files in /blog/static/blog/ so when using them you need to refer to /blog/logo.png and you know for sure it's going to be the right logo.png. Inside the main project, put the files in assets/assets (or call this static/assets instead) so you can refer to /assets/logo.png for the main assets of your project.
Now if you set STATIC_URL = "/static/", this tells django that all static files urls are prefixed with "/static/", i.e. the logo above can be retrieved with "/static/assets/logo.png" and "/static/blog/logo.png". This is required, since it allows to differentiate a url to a static file from urls that Django routes to views.
In practice, in your templates, you'll use {% static 'blog/logo.png' %} so you don't have to hard-code STATIC_URL everywhere.
Now enters webpack. It needs to put its packages assets in any of the above directories, or in its own directory that you **also add to STATICFILES_DIRS. Let's assume the latter: We also have a /webpack/webpack/ folder in our project that's defined in STATICFILES_DIRS, so all webpack assets will be accessed via the url prefix "/static/webpack/".
The worker.js file should be in "assets/assets" and inside your other scripts, if you refer to it via url (to fetch it with an HTTP request), you should refer to it via "/static/assets/worker.js" or using a relative url "../assets/worker.js".
Production
After you deploy, you don't serve your static files with Django anymore but with a webserver, e.g. Apache or nginx. You configure the web server to recognise any request starting with the STATIC_URL ("/static/") and fetch the corresponding file from STATIC_ROOT.
So the first thing to do is to copy all the static files of your django app into a folder/location that's reachable by your web server. That location is outside of your project folder.
Set STATIC_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'static')), which points it to a "static" directory in the parent folder of your project.
Then run python manage.py collectstatic.
This will copy all static files into this new static folder outside of your project. You'll see:
path/to/dir/
|_ static/
| |_ blog/
| | |_ logo.png
| |_ assets/
| | |_ logo.png
| | |_ worker.js
| |_ webpack/
| |_ some_file.js
|_ django_project/
|_ manage.py
|_ main_app/
| |_ settings.py
|_ assets/
| |_ assets/
| |_ worker.js
|_ blog/
| |_ static/
| |_ blog/
...
Now when your web server sees the url "/static/assets/worker.js" it will return the file in "path/to/dir/static/assets/worker.js".
If you let your production server build the webpack files instead of committing them to your source (i.e. they are not inside "django_project"), then you should configure webpack to put them in the correct location "path/to/dir/static/webpack/" because that's where your web server will be looking for them.
Imagine now you move everything to a 3rd party storage provider. You then only have to do two things:
Install and configure the storage backend for that storage provider so that Django knows how to read/write files there (with collecstatic). This might include setting STATIC_ROOT, e.g. STATIC_ROOT = https://my-bucket.my-storage-provider.com/static.
Set STATIC_URL to the URL of the storage provider, e.g. STATIC_URL = https://my-bucket.some_cdn.com/. Here we point to the CDN which you've configured to know where to fetch the files from.
That's all, everything continues to work.

Django pipeline - How to serve static files after running collectstatic?

My project structure
|- myProject
|- manage.py
|- myProject
|- settings.py
|- static // This is my STATIC_ROOT
|- myApp
|- static // This is where I edit and save my static files
So I run the collectstatic command. My css/js files are gathered (and compressed into a single file) into the STATIC_ROOT directory (myProject/myProject/static). So far so good.
First question: How do I serve them?
I tried calling http://127.0.0.1:8000/static/css/styles.css but it returns a 404 file not found error.
I tried adding STATIC_ROOT in the STATICFILES_DIRS but then I get the error:
The STATICFILES_DIRS setting should not contain the STATIC_ROOT
setting.
Second question: Can Django serve "project-wide" static files?
I know that you can serve "application-wide" static files by calling "/static/myApp/my-static-file.css".
But does Django know about myProject/myProject/static the way it knows about the static folder inside each app? Or is it something you need to tell explicitly?
I've read some blog/forum posts saying that you need to change urls.py to but I tried, without success. Do I have to?
I find it pretty hard to find information about static files in Django's documentation and I'm very confused right now.
EDIT: now I am really confused: I tried setting PIPELINE_ENABLED = False and now this url works. http://127.0.0.1:8000/static/css/styles.css but it doesn't help because when PIPElINE_ENABLED is False, it doesn't try to call the compressed css, but all files separately.
I needed to set DEBUG = True and PRODUCTION = False to get it to serve static files. Just the DEBUG = True would not do it.

run 2 websites in one django project

First of all I have found several solutions for my problem but no one fits. So i use Django 1.5 + Python 3.3 + Gunicorn + Nginx as webserver.
My Django project directory look like:
fv/
frontend/
static/
templates/
index.html
models.py
views.py
fv/ -- this is only a folder where logic calculating files are stored
media/
static/
manage.py
settings.py
urls.py
wsgi.py
Now I would like to have the app homepage like frontend but these two apps should be like a standalone website which different domains like www.homepage.com and www.frontend.com. But they should use the same model, because the only model table I would like to share is the Django user model. And every app should have his own template files and urls.py.
I've already read the Django sites framework documentation but i have no idea how to redesign my project dir and arrange other necessary parts like the settings.py, urls.py.
I hope anyone could help me? :)

Adding a second project to a Django project

I'm new to Django. I'm using Django with Eclipse. I've created a Django project using Eclipse (called "Django_Test_Project"). I've also created a PyDev project outside of Eclipse, using the command line (called "polls"). It has models.py, views.py, and tests.py.
I created "polls" using the following command:
manage.py startapp polls
I want use Eclipse to add "polls" as a second project to "Django_Test_Project". How do I do that with a project that was created outside of Eclipse? Eclipse doesn't recognize "polls" as a project, probably because the project files are missing in "polls".
Any help is appreciated. Thanks.
You are working on the Django tutorial, right? First of all, your terminology is not correct. You confuse a project with an application or app for short. It's no surprise that Eclipse doesn't recognize polls as a project, because it's not a project but an app.
In Django 1.4.1, the standard structure for a project called mysite is this:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Your polls app should go in the same directory where the file manage.py is located:
mysite/
manage.py
polls/
__init__.py
models.py
tests.py
views.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
You can just move the polls directory into the mysite directory using the Windows Explorer, Finder, Terminal etc. (depends on which OS you are running on). After refreshing the project view in Eclipse, your polls app should show up. In any case, you should read the Django tutorial more carefully as it basically answers your question already.
Additionally, take a look at this thread that explains the difference between projects and apps in a bit more detail.

Categories