I got an error which is a template that does not exist while I was created a template folder that contains a file. I had done necessary changes in setting.py file.
This should be the hierarchy of your project. Django detects templates like this:
manage.py
project_name/
wsgi.py
urls.py
settings.py
web/
urls.py
models.py
views.py
templates/
web/
home.html
newhome.html
For further info, checkout documentation : https://docs.djangoproject.com/en/2.2/intro/tutorial03/
You should move your templates directory to the root of the project, besides manage.py
Basically, thee BASE_DIR is where your manage.py file lives; you add os.path.join(BASE_DIR, 'templates') into your templates directories, meaning one level up.
on your Templates setting,remove the catalog and edit to this
os.path.join(BASE_DIR,'templates')
Related
i am new to Django and web developement and am struggling with Static Files. I have seen this or similar questions in here before, but nothing seemed to bring me a solution. The Problem is: I can find the static files in Django, but they are not loaded within the template.
My Folder Structure is as follows (everything in a folder named Django):
media_root_folder
static_root_folder
Project
Project
manage.py
mysite
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
templates
static
admin
Project
style.css
In Settings.py I have the following:
DEBUG = True
STATIC_URL = 'static/'
STATIC_ROOT = os.path.abspath(r'E:\LM\Django\static_root_folder')
STATICFILES_DIRS = [
BASE_DIR / "static"
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
'django.contrib.staticfiles' is added as well as Project to INSTALLED_APPS
I also even added this (I found in a Django Forum that this sometimes solves the Problem):
import mimetypes
mimetypes.add_type("text/css",".css", True)
Within urls.py i accordingly added
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Within my tempalte I am using:
{% load static %}
<link rel="stylesheet" type="text/css" href=" {% static 'Project/style.css' %} ">
If I run manage.py findstatic it finds the files and I also can open them directly in the browser. However if I manage.py runserver and load my site (even in incognito mode and after clearing cache and also in multiple browsers) the CSS is not used for the page at all. I only can see the plain html.
If I load the django/admin side the css is loaded and presented clearly.
What am I doing wrong and how can this be fixed?
Do you try to use collectstatic manage command?
$ python manage.py collectstatic
https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/
The Problem was, that in every template I was extending the base.html .
When I was loading the CSS in base.html it worked without any problems.
Following is my django file structure
myproject
myapp
migrations
static
templates
myapp
demo.html
urls.py
views.py
models.py
admin.py
apps.py
tests.py
Scripts
demo_script.py
manage.py
What I want to know is how to execute the script Scripts/demo_script.py (which is outside the application "myapp") from views.py and if it is executed successfully, show "Success" on myapp/templates/myapp/demo.html or else show "Failed to execute" on the same html page.
Thanks in advance!
simply use:
relative_path = "scripts/demo_script.py"
# or
absolute_path = os.path.join(settings.BASE_DIR, "scripts", "demo_script.py")
exec(open(relative_path or absolute_path).read()) # execute the py file
All the paths referenced in Django are relative to the BASE_DIR. BASE_DIR is where your manage.py lies.
I had tried to setup static files in Django but failed.
I have the following directory structure
app
|
|- manage.py
|- requirements.txt
|- static
| |
| |- css
| |
| |- snapweb.css
|
|
}- templates
|- web
|
|- __init__.py
|- settings.py
|- urls.py
|- wsgi.py
/app/web/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
import logging
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
]
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
/app/web/settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Take note that, I didn't run python manage.py collectstatic. As, if I want to run, I will get the following warning, which I have no idea how to solve it.
/app # python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
/app/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel:
So, my problem is, I feel confused on my app behavior.
The following URL works. I didn't expect it to work, as I don't see /app/static/admin/css/base.css file exist. Where does Django pick up the file?
https://localhost:2053/static/admin/css/base.css
I also confused, for the following URL not working. Even though file /app/static/css/snapweb.css is there. Why Django doesn't pick up the file?
https://localhost:2053/static/css/snapweb.css
I wish both URLs, https://localhost:2053/static/admin/css/base.css and https://localhost:2053/static/css/snapweb.css will work. Is there any setup I had missed?
Also, how I can run python manage.py collectstatic successfully, without overwriting my /app/static?
First, you seem to be missing the django distinction between apps and the project (given you have named your project "app"). The project is what has manage.py and settings.py. The apps are what have models.py files.
Given you are using static files from the admin app as well as your own, you are going to need to do some restructuring. Django is looking for static files in STATIC_ROOT. In a "proper" setup, you're supposed to put static files in directories specific to your apps (as opposed to your project). The project static file (pointed to by STATIC_ROOT should have nothing you defined in it since that's where Django is going to put all your files when you run collectstatic.
Try moving your static folder to be a subdirectory of your app folder ("web/static") and running collectstatic.
See the docs for more info.
I have a Django project with the following structure:
root
videos
static
templates
and the STATIC_URL setting in settings.py is STATIC_URL = '/static/'
I managed to use pyinstaller to create a windows executable from manage.py. I can start the Django server but I can't figure out where to put the static files.
When I first started the server it could not find the templates as well, it searched for them in : 'root\django\contrib\admin\templates\videos\' I copied the templates to this folder and it worked. But I can't figure out where to put the static files. I tried putting them in 'root\django\contrib\admin\static' to recreate the original structure. But it seems it doesn't search for them there...
Anyone knows where the static files should go? Or how to define the place where a bundled pyinstaller exe will look for them ?
please see https://docs.djangoproject.com/en/1.10/howto/static-files/
in your urls.py file, you should add something like blew
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^login$', login, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and in your app.spec file, add datas
datas=[('xxx/templates','xxx/templates'),
('xxx/static','xxx/static')],
I think I figured this one out. Like you, I was having the same issue where I could package and build my Django project with pyinstaller, but the static files could not be found when running the project from the built executable. Everything was working fine when I would run the project with manage.py, but this was a head scratcher.
The solution that worked for me was running the collectstatic function and then serving my static files from that directory instead of the app/static/app directory.
If you aren't familiar with collectstatic it essentially searches your project directory for all of the static files in your apps and puts them in a folder on your top level directory that you specify in your settings file.
Here's how to run it.
Here's a link to its documentation.
Now my top-level directory looked like this.
site
app
admin.py
apps.py
models.py
views.py
site
asgi.py
settings.py
urls.py
wsgi.py
media
media files
staticfiles
static files I want to serve (css, js, etc)
In my settings file I specified my static file settings like this...
STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'app\\static\\app')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Then in my urls.py file I included the staticfiles directory like this...
(Note this is the urls.py file in the main site directory not in the app directory if you made one in there)
from django.contrib import admin
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static
urlpatterns = [
path('', include('app.urls')),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In my spec file I was able to add the folder like this...
datas=[('..\\site\\staticfiles\\', '.\\staticfiles\\')]
And then in my html file I was able to use the static files like this...
<script src="{% static 'app/static.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'app/static.css' %}">
Hopefully this helps some people if they've run into the same problem. Also as a reminder be sure to run collectstatic before you build especially if you've modified or changed any of your static files.
python manage.py collectstatic
Cheers!
First make sure that django.contrib.staticfiles is included in your INSTALLED_APPS in settings.py. Then have to insert in your settings.py for example this one:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
I hope definition of BASE_DIR you have in the top of settings.py. BASE_DIR points to a folder, where your manage.py file exists. So if you would have static files in the same dir, then you leave above setting as it is. If not, let's say in the same dir, next to manage.py you will have folder named app, then your settings should look like:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "app/static"),
]
And you can even run:
python manage.py collectstatic
To get admin static files into your static directory.
Hope that helps.
This looks like it might be an issue with where the PyInstaller packed python script looks for static files. They are placed in a temporary folder and need to be accessed by an absolute path. Check out this issue: Bundling data files with PyInstaller (--onefile)
Preface this by saying I have now read multiple posts on this question (including here, here, and here). What I understand is that the static url in settings.py needs a modification for heroku to run these static files. What I need, explained like I am a child, is what tweak to make to these static url when the static directory is nested within the app -- as this was a best practice imparted in a recent tutorial (if this is not the ideal practice I would appreciate being corrected).
Question 1: Should the media files be kept in a directory within the app or at the project level?
Question 2: If the media files are kept within a directory inside the app, like my directory below, then how am I supposed to modify the url in settings.py to load the static files once pushed to heroku?
My project structure is the following:
gvlabs
__init__.py
__init__.pyc
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
manage.py
Procfile
requirements.txt
runtime.txt
welcome
__init__.py
__init__.pyc
admin.py
admin.pyc
apps.py
hello.py
migrations
models.py
models.pyc
static
css
fonts
images
js
templates
welcome
base.html
comingsoon.html
contact_us.html
index.html
post_list.html
tests.py
urls.py
urls.pyc
views.py
views.pyc
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(PROJECT_ROOT,'../welcome')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
#STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_ROOT= os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/welcome/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = ()
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
The main problem with your configuration is the STATIC_ROOT setting. You should change this to something like os.path.join(BASE_DIR, 'static_root').
STATIC_ROOT should point to an empty directory (it doesn't need to exist, Django will create it if necessary) where Django can collect your static files together and do any necessary processing on them before it serves them. It is not the directory where you store your static files.
Regardless of where you put your static files, you shouldn't need to change the STATIC_URL setting. Just leave it as /static/. The main reason for needing to change this is when you're serving static files via a CDN, when it would be set to something like https://my-cdn.example.com/static/
I would keep static files in a directory at the project level. Sometimes, when creating a reusable app it makes sense to bundle everything together by storing its static files in a directory within the app. But most projects I've worked on have kept the main set of static files at the project level.
It doesn't really matter where you put your static files as long as you tell Django where to find them. You do this by adding the path to the directory to the STATICFILES_DIRS setting like so:
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
(Technically, if your static files are in an app directory Django should be able to find them automatically, but let's keep things simple and explicit.)
As a side note: be careful not to use the term "media" here as that has a specific meaning in Django terminology where it refers to user-uploaded files like profile images rather than files that belong with your codebase like CSS and JavaScript files.