Visibility problem for static files -- django - python

I connected static files in django, but they won't connect, can you help?
settings:
STATICFILES_DIRS = [
"web_page/static",
]
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
index.html:
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="{% static "css/standart_template.css" %}" rel="stylesheet" type="text/css" >
<title>HouseVOP</title>
</head>
urls - projects:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('web_page.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urls - app:
from django.urls import path
from .views import FormListView, Success
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', FormListView, name = 'home'),
path('success/', Success, name = 'success')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Before turning here, I searched many sites, so now I may simply not see something. In the first version of the code, I laid the path to the static files along with os.path.join (BASE_DIR, etc., but it did not work ...

try this one:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
dont use web_page/static.no need to.

Related

I am unable to import css and js files from statcfiles in root directory

I have tried everything suggested on the entire internet and stuck here for over 5 days
Here are my settings.py
STATIC_URL = '/static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I have a staticfile directory, i created with
python manage.py collectstatic
and I have all my css in that file
here are my urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here are my app urls
from django.urls import path
from accounts.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', SignUpView.as_view(), name='signup'),
path('otp/', OTPView.as_view(), name='otp'),
path('login/', LoginView.as_view(), name='login'),
path('home/<int:id>', HomeView.as_view(), name="home"),
path('logout/', UserLogout.as_view(), name="logout"),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and here is my template base.html that I am inheriting in every other template
{% csrf_token %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/User_Authentication/staticfiles/admin/css/responsive.css">
</head>
I am getting this error
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:06:31] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Why am i still not able to use staticfiles
From the docs, static-files
{% load static %}
{% csrf_token %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'admin/css/responsive.css' %}">
</head>
You need to load it first, the order of loading these three might have to be altered.
You can run the app, go to the page, inspect and find out exactly what url django translates this into.
You are basically asking django/jinja to dynamically convert this static path to the corresponding static url instead of hardcoding it.
Note: {% load static %} needs to be added in every template file that uses "{% static '...' %}". Just adding it in base.html or an equivalent file that other templates extend from is not going to work.

Django: Static Image Not Loading

I'm having issues getting an image to load on my Django web application. I have used the online documentation and tried peoples suggestions that I found on Stackoverflow but I am still not having any luck getting it to load. Here is what I have:
Settings.py:
STATIC_DIR = os.path.join(BASE_DIR,'static')
INSTALLED_APPS = [
...
'django.contrib.staticfiles'
]
STATIC_URL = '/static/'
STATIC_ROOT = [STATIC_DIR,]
Urls.py:
from django.urls import path
from dir_app import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'dir_app'
urlpatterns=[
path('', views.tradesmen, name='tradesmen'),
path('register', views.register,name='register'),
path('user_login', views.user_login,name='user_login')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Index.html: (Gist)
<!DOCTYPE html>
{% load static %}
<html>
<head>
</head>
<body>
<img src="{% static 'dir_app/tools.png' %}">
</body>
</html>
Here is where the image is stored:
DEBUG should be true if want to use django.conf.url.static
https://github.com/django/django/blob/926148ef019abcac3a9988c78734d9336d69f24e/django/conf/urls/static.py#L23
STATIC_ROOT should be a string, not a list.
https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATIC_ROOT

Requesting /admin but return to the same page

Hello there i m looking to fix a problem i m working on django right now the issue is that when i try to go to the admin area login it returns me to the same index.html page
my code:
urls.py on main folder
from django.conf.urls import url,include
from django.contrib import admin
from first_app import views
urlpatterns = [
url('',views.index,name="index"),
url('first_app/',include('first_app.urls')),
url('admin/', admin.site.urls)
]
urls.py on first_app folder
urlpatterns = [
url('',views.index,name="index")
]
the views.py on first_app folder
def index(request):
my_dic = {'insert_me':"Hello Jinja"}
return render(request,'index.html',my_dic)
index.html file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static 'css/style.css'%}">
<title>Django Page</title>
</head>
<body>
<p>{{ insert_me}}</p>
<h1>a picture </h1>
</body>
</html>
You are mixing up path with url, if you are using Django < 2.0, then use url with regex, like this:
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^first_app/', include('first_app.urls')),
url(r'^*$', views.index, name='index'),
...
]
And if you are using Django >= 2.0, then use like this:
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('first_app/',include('first_app.urls')),
path('',views.index,name="index"),
]
More information can be found in documentation.

CSS is not showing

I am learning Django and while I was trying to load static CSS files, it did not show any output. When I run server I only get HTML as my output.
Here is my code
settings.py code
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'bloodnepal/bloodnepal/static',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
{% load static %}
<meta charset="utf-8">
<title>Bloodnepal</title>
<link rel="stylesheet" href="{% static 'css_files/style.css' %}">
<link rel="stylesheet" href='{% static "css_files/link.css" %}'>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Fira Sans Extra Condensed' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Nanum Myeongjo' rel='stylesheet' type='text/css'>
</head>
</html>
url.py
from django.contrib import admin
from django.urls import path
from . import views #imported the views.py files here for our pipeline
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',views.home,name="Homepage"),
path('donate/',views.donate,name="Donatepage"),
path('organization/',views.organization,name="Organizationpage"),
path('getinvolved/',views.getinvolved,name="Getinvolvedpage"),
path('about/',views.about,name="AboutUspage"),
]
urlpatterns += staticfiles_urlpatterns()
If you store your CSS inside your project folder under "static" directory then the following code for settings.py should work:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
....
STATIC_URL = '/static/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
]

Python Django (static files/change ip server)

I have one problem. When i change ip adress for server, then my css (Bootstrap) don't work.
urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Project.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^home/', 'Blog.views.my_blog')
)
if settings.DEBUG == True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
base.html
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head lang="ru">
<title> Savichev's site </title>
<!-- Bootstrap -->
<link rel="stylesheet" href={% static "css/bootstrap.css" %}>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset="UTF-8">
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
What's wrong ?
CSS work correctly only on ip 127.0.0.1:8000
I suppose, with the little information you gave us, you need to provide a STATIC_ROOT variable in your settings.py.

Categories