Django 1.4 has updates its default project layout, so my new project layout becomes
root
manage.py
/project
/settings.py
/app1
/static
/app2
/static
Now I'd like to use JavascriptMVC framework for my project, where is the best place to place it? It's huge(39M) just for the bare framework.
I don't want to have a framework inside each of my app. So, is there a way to host it in a central place and used by all my apps?
I personally put my project-wide static files in the top level directory (root/static/) and add it to STATICFILES_DIRS
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-dirs
It makes much more sense to me to keep it in the same level as apps. I only put project configuration related files in my root/project/ equivalent folder like settings.py, urls.py, local_dev flags / local settings, etc.
Related
I am using Django to build a SSR website and using Django to serve static files. I also built some Web Components using lit-element and Typescript. I would like to avoid the complexity of Webpack and use Snowpack instead. Components are stored at /static/js/components.
To use the components, I need to (1) transpile them to Javascript, (2) make available their dependencies (e.g. lit-element) and (3) move the transpired files as well as the _snowpack folder to Django's /static/ folder.
Using just snowpack build does both but creates a build folder with a complete copy of the application. It is my understanding that buildOptions.out only moves the output folder (thereby overwriting static/ altogether if buildOptions.clean===true). I guess it would be possible to script the copying of the necessary files and delete the rest of the build folder immediately, but that strikes me as quite inelegant.
Is there a configuration to only create _snowpack and the transpiled files to a specific directory?
The Django staticfiles module supports having multiple directories for static files, and making them all available under the same /static/ URL.
That way, you won't have to copy those different type of assets into the same folder before being able to serve them with Django.
I'm wondering where do you Django developers keep your media folder. I'm currently developing a project located at ~/apps/django-project. I am not using continuous delivery at the moment, but when I have to update the project, I ssh straight into the remote machine and pull the updated files using git (that's because it is placed in the project folder). The problem is that the media folder is always updating and I'll have to track them using git, which is not something I want to do. I don't think I'd like to .gitignore them too.
Would it be a good idea to structure the project like that?
Project: ~/apps/django-project
Media: ~/media
Statics: ~/static
If that's a good idea, could you give me a hint about how to set up my settings.py MEDIA_ROOT and STATIC_ROOT in order to accomplish this?
If that's not a good idea, I'd like to hear from you how would one structure the project and what other good principles should I take into account. (also, development / production tips are welcome)
Django3 with Python3.7
I create a folder public in my root directory and there I add the media and static directories
public/
media/
static/
I also add the specific paths to .gitignore, so they don't conflict between environments.
public/media
public/static
The good thing about this approach is that if you are using a webserver like nginx or uwsgi you can set the document root to public and serve any static file by default and resolve to django any other path that is not a file in public.
For example in nginx I do something like this.
server {
root /var/sites/myproject/public;
location #djangoapp {
proxy_redirect off;
proxy_pass http://localhost:8000;
}
location / {
try_files $uri #djangoapp;
}
}
This is very convenient because is easy to reason about public, everything that is in that folder will be served statically. For example I put there my robots.txt file and some others that I know they just need to be simple serve. It defaults nicely and really fast to django for any other request that is not an static file in public
public/
media/
attachs/
users/
static/
admin/
css/
js/
robots.txt
humans.txt
manifest.json
favicon.ico
I even once added support for .php files where I just put the files there and add the .php setup as an extension rule in nginx.
I am new to Mezzanine and not an expert in Django. I am trying to make changes in the default Mezzanine theme. I am doing these steps to override default Mezzanine theme,
python manage.py collectstatic
A new static folder is created which stores all the static files across the project in this folder (STATIC_ROOT). This includes Mezzanine default static files as well from virtualenv/lib/python2.7/site-packages/mezzanine/core/static/
Then I run
python manage.py collecttemplates
A new templates folder is created which stores all the templates across the project in this folder.
Now when I make changes in files from /templates directory I get to see those changes in development server.
but when I make changes in /static directory files I do not see those changes in development server.
to make changes to css files I have to go to
virtualenv/lib/python2.7/site-packages/mezzanine/core/static/ path and then only I can see the changes in development server.
I want to know what am I doing wrong. I do not want to make changes in mezzanine css files, I want to overwrite them form static_root.
In mezzanine and in django sites in general, you should not edit files in the /static folder directly since these will be overwritten each time you use the collectstatic command.
Each of the templates Mezzanine provides can be found in the templates directory of each Django app that Mezzanine is comprised of. E.g the base template in mezzanine/core/templates/base.html.
To edit the templates you’d like to modify, copy them into your project’s templates directory and modify them there. You can also use the collecttemplates command to copy templates over automatically. Run python manage.py collecttemplates --help for more info.
The mezzanine template lookup system is described here. This post may also be useful
OKay, After some research I realized, This is not the way to overwrite templates. You want to copy static folder from Mezzanine package and paste it in an app where you want to use it, then edit that copied files. This is written in documentation for mezzanine, I missed that part.
I use django for the backend, in the frontend I use vue.js, so 99% of my CSS it's handled by vue.js, however I need a simple base.css for some customization in the landingpage and few things like this.
Normally in django I would put the file inside app/static/app/base.css then do collectstatic and get it under static/app/ for production.
I would like to avoid to keep it under an app as it's just a file. I'm trying adding a folder under my main "static" folder. But it seems in development django in not fetching it at all, it fetches directly and only static files from apps.
How can I tell django to fetch it directly from the static main folder as it would do in production?
i.e. I want to add a folder called main in my root (where manage.py is) and use only that to store my static files for both production and development, without passing through the single apps.
You can tell Django to look for static files in other directories by using STATICFILES_DIRS settings.
Just add the following code in your settings.py and it should work:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
There was somewhere I read couple of years ago (Two scoops of Django 1.4?) suggested having an "apps" folder inside the Django project which hosts all the apps.
Repo folder
-- Project root folder
-- apps
-- app 1
-- app 2
-- settings etc
But just had a quick read of "Two scoops of Django 1.8" there is no mention of the "apps" folder in the preferred project structure.
Repo folder
-- Project root folder
-- app 1
-- app 2
-- settings etc
What have I missed? And why did they remove this folder?
I personally have gone back and forth on this one.
The new default layout of django is somewhat confusing - having two folders - one for the core software and one for your app.
The thing I have had issue with is by adding your app to the top level of the PYTHONPATH, another package installed from say someplace like PyPi will interfere with yours. For that reason alone I suggest 'namespacing' your apps inside the apps folder. I put 'namespacing' in quotes as by having something like 'myspecialproject' with 'myspecialproject.apps.payments' is a whole lot better than having 'payments' at the top level which will likely get clobbered by some other package.
So yes, I suggest an apps folder inside your project.
like #marcusshep said is just preference, anyway if you want to keep your apps inside this folder you can add this line after BASE_DIR var sys.path.insert(0, os.path.join(BASE_DIR, 'apps')). With this you don't have to use apps.app..., is like that folder doesn't exist