django adding part of url to static css path - python

I have my css located in
static/css/boostrap.css
I currently have 2 views. A login view and a dashboard view.
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 import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'logins.views.login', name='login'),
url(r'^dashboard', 'dashboards.views.dashboard', name='dashboard'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
When i load up the login page it looks for the static files in
/static/css/login.css
But when i load the dashboard it looks for it in
dashboad/static/css/bootstrap.css
login.html css reference
<link href="static/css/signin.css" rel="stylesheet">
dashboard.html css reference
<link href="static/css/dashboard.css" rel="stylesheet">
Its adding part of the URL to the path off the static files and can not for the life of me figure out how to stop it.
settings.py
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
'/Users/chrismeek/Documents/Python/virtual/src/static/templates',
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/static-only'
MEDIA_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/media'
STATICFILES_DIRS = (
'/Users/chrismeek/Documents/Python/virtual/src/static/static',
)

You are using relative URLs to link to the assets: so they always start from the current page's directory.
Make sure you always use a leading slash:
<link href="/static/css/dashboard.css" rel="stylesheet">
Even better, use Django's static tag to automatically output the value of STATIC_URL, whatever it happens to be:
{% load static %}
...
<link href="{% static "css/dashboard.css" %}" rel="stylesheet">

Related

Image is not showing up on my website (django)

I am new to django. I wanted to upload an image but it doesn't show up on website. Instead, it shows a broken image icon. I tried to use load static block but it still doesn't work.
My html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Website</title>
</head>
<body>
<h1>Hello</h1>
{% load static %}
<img src="{% static 'webdev/static/webdev/images/images.jpg' %}">
</body>
urls.py file:
from django.contrib import admin
from django.urls import path
from homepage import views
urlpatterns = [
path('', views.home_page, name='home'),
path('admin/', admin.site.urls),
]
views.py file:
from django.shortcuts import render
from django.http import HttpResponse
def home_page(request, *args, **kwargs):
return render(request, 'home.html', {})
file tree: https://i.stack.imgur.com/EYxNI.png
The documentation on Serving static files during development explains how to set up static files. You need to add the views that serve the static files to the urlpatterns:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from homepage import views
urlpatterns = [
path('', views.home_page, name='home'),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This will serve static files when DEBUG is set to True. In case of production (DEBUG = False), Django does not serve static files. Then you should configure the webserver (nginx/apache/…) to serve the static and media files, or work with a CDN.
Furthermore the path is:
<img src="{% static 'webdev/images/images.jpg' %}">

Static files are not loading in Django

Why I am receiving files can't be found error while loading all the static files even after setting everything correctly.
ERROR
CODE:
Admin Section
- settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '')
- urls.py
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('Welcome.urls')),
path('auth/', include('Authentication.urls')),
path('ad/', include('Ads.urls')),
path('user/', include('UserDashboard.urls')),
path('admin/', include('AdminDashboard.urls')),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Apps Section
- template
<link href="{% static 'css/user/style.css' %}" rel="stylesheet">
- Folder Structure
You get this error because the Django project searches your static folders in the wrong place.
This will work:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

how can I add a url for my template in django python?

i use a template im my django homepage.(http://127.0.0.1:8000/)
I linked statics files in static folder and formed like {% static .....}
when i path it in urls in the main page it work correctly.
but when I give it an slug like 'p/' it doesn't work.
because it search for statics in p folder:
"GET /p/static/post/js/scripts.js HTTP/1.1" 404 2598"
my app name is "post" .
please help?"
<link href="{% static 'post/css/bootstrap.css' %}" rel="stylesheet">
<link href="{% static 'post/css/fontawesome-all.css' %}" rel="stylesheet">
<link href="{% static 'post/css/swiper.css' %}" rel="stylesheet">
<link href="{% static 'post/css/magnific-popup.css' %}" rel="stylesheet">
<link href="{% static 'post/css/styles.css'%}" rel="stylesheet">
'
this my setting:
STATIC_URL = '/static/ MEDIA_URL = '/media/' STATIC_ROOT = os.path.join(BASE_DIR, "static") MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this my urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('post.urls')),]'
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]
this is my views:
'''
from django.shortcuts import render
from django.http import HttpResponse
from .models import app_post
def hello(request):
html = "<h>salam django<h>"
return HttpResponse(html)
def page(request):
return render(request,'post/index.html')
'''
this is my app urls:
urlpatterns = [
path('',page,name = 'page'),
path('h/',hello,name = 'hello'),
]
Add this in your settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
and then run command python manage.py collectstatic

Django and apache not serving static files

I made a simple page to serve a static CSS in django but I always get a PAGE NOT FOUND error.
Views.py:
def login_user(request):
return render_to_response('new_file.html')
urls.py:
from django.conf.urls import patterns, include, url
from bookmarks.views import *
import os
from django.conf import settings
# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()
site_media=os.path.join(os.path.dirname(__file__),'site_media')
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'hello.views.home', name='home'),
#url(r'^hello/', include('hello.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#url(r'^admin/', include(admin.site.urls)),
(r'^$', main_page),
(r'^login/$', login_user),
(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT})
)
The template HTML:
<html>
<head>
<title>THIS IS MY PAGE</title>
</head>
<body>
<h1>NEW PAGE</h1>
<link href="/site_media/newstyle.css" rel="stylesheet" type="text/css" />
<p>{{ page_body }}</p>
</body>
</html>
The CSS:
#body {color:red;}
The settings.py site_media part:
MEDIA_ROOT = '/srv/www/hello/site_media/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/site_media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = '/srv/www/hello/site_media/'
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/site_media/'
Try like this:
<link href="{{STATIC_URL}}css/newstyle.css" rel="stylesheet" type="text/css" />

Django template can't see CSS files

I'm building a django app and I can't get the templates to see the CSS files... My settings.py file looks like:
MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')
MEDIA_URL = '/media/'
I've got the CSS files in /mysite/media/css/ and the template code contains:
<link rel="stylesheet" type="text/css" href="/media/css/site_base.css" />`
then, in the url.py file I have:
# DEVELOPMENT ONLY
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/media'}),
but the development server serves the plain html (without styles). What am I doing wrong?
--
OK - I got it working based on what you folks have said. The answer is:
settings.py:
MEDIA_ROOT = 'd://web//mysite//media//' #absolute path to media
MEDIA_URL = '/mymedia/' #because admin already using /media
site_base.html:
<link rel="stylesheet" type="text/css" href="/mymedia/css/site_base.css" />
urls.py
from mysite import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^mymedia/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
And voila! It works.
in the "development only" block in your urls.py you need to change
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/media'}),
to...
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
ADMIN_MEDIA_PREFIX is set to \media\ by default, and is probably 'stealing' the path. Change that setting, or use a different one for non-admin media - eg site_media or assets.
On the dev server, I like to cheat and put the following in my urls.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^includes/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/static/files'}),
)
That way anything in the project under the "/includes" folder is server by the dev server. You could just change that to "/media".
It also worked for me, thanks guys !!
settings.py
MEDIA_ROOT = '/home/pi/ewspaces/ws-classic/xima/media'
MEDIA_URL = '/statics/'
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^statics/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
inside templates:
<link type="text/css" href="/statics/css/base/jquery.ui.all.css" rel="stylesheet" />
I had a similar problem when I was trying to get jQuery to work. My fix was to add an alias to my Apache httpd.conf file that pointed to the folder containing the .js. You could do the same with your CSS folder.

Categories

Resources