Reputation: 137
I come from an Mean, Express, Angular and Node (MEAN) stack background. I really liked how angular has separate components and then we can style each components and then nest them in each other. It is just more organized and way more shareable when in companies. It gets easier to actually understand the structure. Can I do this in django since I notice that my css is supposed to be all in one static file. Is it possible?
NOTE: Angular not Angular.js
Upvotes: 0
Views: 54
Reputation: 137
We can use the webpack method. Could not find this tutorial easily on googling.
Upvotes: 1
Reputation: 15701
While all your static files do need to be contained in one folder in production, they can be in separate places in development.
Development: Separated
You can have separate static files for each app within the project, but each should contain a folder with the name of the app. So the path to the static file style.css
in the app myapp
would look like: myproject/myapp/static/myapp/style.css
. You can also put more folders within the myproject/myapp/static/myapp
to separate images, JS, and CSS, for example.
To have this work you need to make sure that django.contrib.staticfiles
is in INSTALLED_APPS
. Then make some edits in settings.py
and set STATIC_URL = '/static/'
and STATICFILES_DIRS=[os.path.join(BASE_DIR, 'static')]
, so that Django knows the URL to refer to as well as the folders to check for static files.
Production: Collected
The above will only work if DEBUG
is set to True
, so it will only work in development. To have it work in production, you need to set STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
, so that Django will know to look in myproject/staticfiles
to find static files.
Then just run python manage.py collectstatic
and Django will go through all your apps and pull everything from the folders called static
within them and place everything in STATIC_ROOT
.
This is why it's helpful to use the convention of putting a folder called myapp
within static
in myapp
—because they all end up in the same folder.
See the docs for more: https://docs.djangoproject.com/en/2.0/howto/static-files/
Upvotes: 1