Can I have an organized way to work with django? - django

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

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/

We can use the webpack method. Could not find this tutorial easily on googling.

Related

Django versioning static files at collectstatic

I'm looking for a way to add a version to all the static files found during a collectstatic.
The goal is to force reload of cached static file. For example if the static file source is src="/jquery/jquery.min.js" I want after the collectstatic to add the date to have src="/jquery/jquery.min.js?date=2015-10-1T10:31:42"
What is the best way to do that ?
Edit : I'm using django 1.4.2
Edit 2 : Eventually, I've updated my app to django 1.8.5
Using ManifestStaticFilesStorage could help.
One possibility is to use django-versioned-static:
This little apps helps to manage your staticfiles when you need to minify them for production use. Moreover, it is capable of versioning the assets so that whenever you need to alter the static files, users won’t be given old file from the browser cache.

What is the best practice for serving static files in Django currently

I've found plenty of advice for how to tackle static files in Django 1.x. Is there a best practices way to go about doing so?
There are may approaches to serving static files in Django, but Django 1.3 introduced a new option to handle them. Basically, you can define specific directories at a project level or at an app level that contain static media. Then via a management command, 'collectstatic', you can copy all of the static files in your project directory to a separate directory (likely external to your project) that is served by your webserver. This solves a lot of complications of the previous approaches.
1) It allows 3rd party apps to easily include static files in a standard way. No more creating symlinks from your webserver directory to locations inside of individual python/django modules.
2) It gives you more control over where your webserver can host its static files. For example, you can define all of your static media inside of you project under version control, but then copy it all to an external location anywhere on the filesystem. This prevents having to point your webserver to a location inside of your project.
3) Is splits out static media that that will never change from static media that is uploaded by a user using something like a FileField. This is good because you likely want to keep site-level static media in version control (to be installed on your dev servers), but user-submitted content will upload to a different directory (unlike versions of django prior to 1.3).
I think this functionality used to be a 3rd party module that was merged into core.
Here are the docs:
https://docs.djangoproject.com/en/dev/howto/static-files/
Basically there are a few new settings.py variables:
STATIC_ROOT - This is a directory on your filesystem that will be served by your webserver. When you run ./manage.py collectstatic, all of the static files from your project and it's apps will be copied into the directory that you specify here.
STATIC_URL - This is the url that you will set to represent what url base your content will be based from. If you set this value to "/static/", then "/static/" will prefix all urls of static media that included. You will also use this variable in your templates. For example, in your template, you could specify something like:
<img src="{{ STATIC_URL }}logo.png">
The STATIC_ROOT settings variable specifies where static files are copied to, but you also need to provide a location of where static files are copied from. This can be done by creating a directory in your individual Django apps called "static//", similar to what you would do for templates within an app. Upon running the collectstatic command, Django will copy all of the static files from all of the "static/" directories in all of your apps to the STATIC_ROOT directory. You can also use the STATICFILES_DIRS settings variable to define project-level directories to copy static media from.
Though there are many ways to serve static media, I think this is a nice api set in place by Django that will help with better integrating 3rd party modules. The fact that this feature is now included in core could give some indication that this approach may have gained some momentum.
Hope this helps,
Joe

Django: How to use static files (simple case, jquery)

I am trying to use jQuery on a Django site. I need to include the jQuery.js library. I have read a lot about Django static files, but I don't think anyone has asked this particular question. I have only three static files to serve: jquery.js, anothersmallfile.js, and styles.css. The Django docs on static file serving say:
"For small projects, this isn’t a big deal, because you can just keep the static files somewhere your web server can find it. link
I would like to "just keep them somewhere my webserver can find them" because elsewhere the Django docs clearly state (warn) that their static-files serving method is only for a development environment. I only have a few static files and I just want the simplest secure solution.
Unfortunately I can't get it working. No matter where I put the files, Django can't find them. Debugging through Chrome web developer console I see I'm getting a 404 error:
GET http://127.0.0.1:8000/templates/polls/jquery.js 404 (NOT FOUND)
I am new to running a server. Do I A.) need to tell my urls.py file where to find static files? or perhaps the problem is B.) that I have misunderstood this issue - Django is my webserver (for production) so right now I must use the Django static files solution?
Doesn't seem like it ought to be very difficult to get my templates to simply recognize a .js file that's in the same directory as they are. Am I missing something?
Edit, before I get more downvotes: I am talking about this passage from the page linked above:
///////////////////////
Django developers mostly concern themselves with the dynamic parts of web applications – the views and templates that render anew for each request. But web applications have other parts: the static files (images, CSS, Javascript, etc.) that are needed to render a complete web page.
For small projects, this isn’t a big deal, because you can just keep the static files somewhere your web server can find it. However, in bigger projects – especially those comprised of multiple apps – dealing with the multiple sets of static files provided by each application starts to get tricky.
That’s what django.contrib.staticfiles is for: it collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.
///////////////////
Emphasis added
So if that's what django.contrib.staticfiles is for, what's the simpler solution? I dispute that this is a repeat of prior questions.
You need to read that documentation more closely. That warning is for production. In development, you do use that static-serving method, ie putting it in your urls.py. And, that documentation will also show that the templates directory is not the right place to put them: a separate static or media directory is.
Edit after comment I really don't understand your comment. Either you do it in development via the static serving view, or you use your production server. But you say you don't have a production server. When you get one, whether it's Apache or Nginx or whatever, you put your static files in a directory and tell that server to serve files from there. That is the simple solution. The staticfiles app, exactly as in the docs you quoted, are for when you've got lots of files in different apps (and it simplifies the move from development to production, not complicates it as you seem to think).
Suppose your app is www.
setting.py -> STATIC_ROOT = 'static/'
make dir www/static
make file www/static/some.html
in browser localhost:8000/static/some.html
That's all.

Using upload_to with STATICFILES_DIRS

Is it necessary to appoint MEDIA_ROOT, if indicated STATICFILES_DIRS? This issue arose when loading images - upload_to formed using MEDIA_ROOT and ignores STATICFILES_DIRS. If actually did not necessarily how to use upload_to with STATICFILES_DIRS?
Media and Static files might seem similar on first glance, but when you dig deeper you will find that Django draws a fine line between both. While Media usually refers to files uploaded by users, static files are created and bundled together with django apps.
The idea behind static files is, that upon release you can call
./manage.py collectstatic
and have all your static files from your apps (even 3rd party ones that live in egg files) collected into a given directory your HTTP server can serve directly (without any django/wsgi in the middle) for best performance.
The same holds true for Media files, but they are uploaded by users and not created by you or other app developers.
Hope that eases your confusion :-)
I recommend you take a look at the excellent documentation at the Django homepage:
Static files: https://docs.djangoproject.com/en/dev/howto/static-files/
Managing stored files: https://docs.djangoproject.com/en/1.3/topics/files/

How to manage static files in django project with many apps?

I am developing a web app based on django for a while. At the beginning, I just put all the static files such as css, js and some icons in one folder and served the folder as static files. While the project evolves, more and more apps added, and there are too many files stored in the folder.
Therefore, I would like to reorganize static files. What would be a good way? Should I classify files according to the apps they belong to or classify them by css, img, and js, or classify by apps first, and then by their types.
You could probably do a mix. Project wide templates and static files could be stored in one central location. And all the app related files would be stored in a dedicated static-subdirectory in each app's folder.
If you use Django 1.3 contrib.staticfiles makes this really easy.
It even provides a handy collectstatic command, that fetches the static files from all your apps and stores them in a central, configurable location, where you can serve them from.
I've had a similar project and organized by app first and then by type, just like you would do with templates. To me it seems more consistent.
Generally I go from general to specific for organization,which would suggest an organization from apps to file types. I would also recommend taking any files that are shared across apps and moving them into a shared hierarchy.