django CSS & Javascript not loading in HTML files?, error 404? - django

I have put some static files for CSS Javascript and Jquery in my file static.
I have put load static in my HTML and it doesn't respond properly to the HTML.
Do you have anything else to do with it?
output error
setting.py file

Problem is that you have not added /static/ urls to your urls.py and set STATIC_ROOT in your settings equal to path to your static files:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
You can see it in docs: https://docs.djangoproject.com/en/3.2/howto/static-files/#serving-static-files-during-development

Related

Django project is not reading my static css file

I am working on a django project and for some reason the style.css file is not read. However, my static images are working. Not really sure what the issue is, any help is greatly appreciated. There are also no errors showing in the console and terminal..
Link to my github repository
https://github.com/Amanuel763/petwebsite
Add STATIC_URL and STATIC_ROOT to your urls.py. See more about Serving static files during development[Django-doc]
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

How do I get Django to load my favicon from my React build directory?

I am using Django and React to create a web application. When it comes to my React Development server, my favicon.ico loads like it should, but when I build my files, my Django development server doesn't find and render my favicon, and I have no idea why. I've tried renaming my favicon and changing the file type to .png. When I put my favicon into my static directory, and change the file name from favicon to some thing like "icon.ico", then it loads properly. But, I can't have my favicon in my static directory because CRA won't copy it into that directory when it builds. It's probably something small and simple so I'll show y'all all of the related files. Thanks for any insight!
EDIT: I tried collectstatic , but that did not help
EDIT 2: using {% load static %} but this doesn't work because my favicon is in the same directory as my html file. I should also clarify that import links to the parent directory that holds both my html file and my favicon file don't register for some reason.
The import in my build/index.html: <link rel="shortcut icon" type="image/x-icon" href="./favicon.ico"/>
urls.py
from django.contrib import admin
from django.urls import include, path, re_path
from django.conf.urls import include, url
from backend import views
from django.views.generic.base import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('backend.urls')),
path(r'^api/<int:pk>$', include('backend.urls')),
path(r'^api/<int:pk>$/', include('backend.urls')),
url(r'^.*$', views.index),
]
my settings.py static settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(FRONTEND, 'build/static/'),
]
CORS_ORIGIN_WHITELIST = (
'http://localhost:3000',
)
Also, let me know if the problem could be in another file
The problem is that the django url routing has no access to the favicon.ico since CRA puts it in /build and all your static files are served from /build/static.
Assuming you haven't ejected, a hacky solution is to copy the favicon.ico into the static folder in your build step in package.json scripts section:
"build": "react-scripts build && cp build/favicon.ico build/static/favicon.ico"
and then in the html template in /public, set the favicon.ico url to
<link rel="shortcut icon" href="/static/favicon.ico">
instead of %PUBLIC_URL%/favicon.ico.
If you have ejected the CRA, you can update your webpack config to move the favicon.ico directly into /build/static

Django Models.filefiled file dosent exist

I wish to access the file i upload using models.FileField(), but its shows different path when i click on the link provided in the admin page. May i know what's the problem here?
Updated my code, but the url dosent seem corret. getting a 404 code error.
you need to declare MEDIA_ROOT = os.path.join(BASE_DIR,'media')
and add them to the urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Loading images in a static file

So I have completed the Django tutorial, play around with it some, and feel that I have a good understanding of Django. The only problem I am facing is that when I try to display a static page for a landing page, I cannot seem to get any images to load.
First off I have tried two different methods of displaying a static landing page.
First:
# main app urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="landing/index.html")),
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
)
This is the main app's urls.py and I use the TemplateView to display the static index.html which is located at 'my_app/templates/landing/index.html'.
This is all and well until I try to add a static image into the index.html file. No matter where I put the image file I cannot seem to get it to display. In the index.html template I have tried different methods of using static as well as trying different direct paths without the need for the embedded python code. How am I supposed to display images and where should they be located?
The second method I found that worked for just displaying the static page (not the image) was to create a new app called landing and have that simply display a static page from the urls.py in the same manner(using TemplateView). Is this method better? I still had the same problems in displaying an image within the static page as the first method, which makes me think it has something to do with the TemplateView.
Am I doing this completely wrong? What are my options? I am using Django 1.5.1.
Thanks in advance!
It is a bit awkward to serve statics files with Django. This is because they have the conception that static files such as images must be in another domain or server for performance/security reasons.
To serve static content with django do this:
# urls.py
# add this lines at the end
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Then in settings.py define the directory where the images/css/js and similar static contents are:
# settings.py
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# this assumes your static contents is in <your_project>/static/
os.path.join(PROJECT_ROOT, 'static/'),
)
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'
Note: You may also consider images as media and place them in your media/ folder.
That should get static files served with your app. Just reference them like this href="/static/xxxx.jpg" or href="/media/xxxx.jpg".
Hope it helps!
This blog explains serving static file and might help:
http://agiliq.com/blog/2013/03/serving-static-files-in-django/

Django favicon.ico in development?

How can I serve favicon.ico in development? I could add a route in my urlconf, but I don't want that route to carry over to the production environment. Is there a way to do this in local_settings.py?
The easiest way would be to just put it in your static directory with your other static media, then specify its location in your html:
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}"/>
My old answer was:
You can set up an entry in your urls.py and just check if debug is true. This would keep it from being served in production. I think you can just do similar to static media.
if settings.DEBUG:
urlpatterns += patterns('',
(r'^favicon.ico$', 'django.views.static.serve', {'document_root': '/path/to/favicon'}),
)
You also could just serve the favicon from your view.:
from django.http import HttpResponse
def my_image(request):
image_data = open("/home/moneyman/public_html/media/img/favicon.ico", "rb").read()
return HttpResponse(image_data, content_type="image/png")
This worked for me:
from django.conf.urls.static import static
...
if settings.DEBUG:
urlpatterns += static(r'/favicon.ico', document_root='static/favicon.ico')
From the docs:
from django.conf.urls.static import static
urlpatterns = patterns("",
# Your stuff goes here
) + static('/', document_root='static/')
There doesn't appear to be a way to serve a single static file, but at least this helper function is a wrapper which only works when DEBUG = True.
I use this:
from django import conf
from django.conf.urls import static
...
if conf.settings.DEBUG:
urlpatterns += static.static(
r"/favicon.ico", document_root=conf.settings.STATIC_ROOT / "favicon.ico"
)
Well, you can create your own loader.py file, which loads settings you want to override.
Loading this file should look like this:
try:
execfile(os.path.join(SETTINGS_DIR, 'loader.py'))
except:
pass
and be added at the end of settings.py.
This settings should not be commited into production server, only should appear on development machines. If you are using git, add loader.py into .gitignore.