I'm trying to load the css files in Django from myapp/static/myapp/css but I can't get it to work. Chrome console says it couldn't get the files (404).
Settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(__file__)
...
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
urls.py:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^projects/', include('projects.urls', namespace='projects')),
url(r'^home/', include('home.urls', namespace='home')),
)+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
home urls.py:
from django.conf.urls import patterns,url
from projects.views import DetailView, ListView
urlpatterns = patterns('',
url(r'^(?P<pk>\d+)/$', DetailView.as_view(), name='detail'),
url(r'^$', ListView.as_view(), name='list'),
)
base.html:
<link href="{% static 'home/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
<script src="{% static 'home/js/jquery-2.1.1.min.js' %}" type="text/javascript"></script>
I found out the code works perfectly well. The problem was that I forgot to put the home app in INSTALLED_APPS.
Thanks for your time.
Related
I want to show an uploaded file :
{% for post in posts %}
<img src="{{post.image.url}}">
<h3>{{post.title}}</h3>
<button type="button" class="btn view-project-btn">view more</button>
<div class="item-details">
<div class="description">
<p>{{post.description}}</p>
</div>
<div class="info">
<ul>
<li>created - <span>{{post.date_creation}}</span></li>
<li>le theme de recherche -<span>{{post.sous_titre}}</span></li>
{{post.body|safe}}
<iframe src="{{post.file.url}}" width="100%" height="500px"></iframe>
</ul>
....
{% endfor %}
this is my views
def home(request):
posts = Post.objects.filter(active=True)
context = {'posts':posts}
return render(request,'base/index.html',context)
but it doesn't work it works for imagefield {{example.image.url}}, this is the attribute in my model file = models.FileField(null=True, blank=True,upload_to="sources")
thanks for your help and suggestions
the error :
Not Found: /sources/file1.pdf
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('post.urls')),
path('ckeditor/', include('ckeditor_uploader.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root= settings.STATIC_ROOT)
in my settings
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR,'static/media')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
here is the issue in your views your are posts in for calling the file but u are using post instead of posts
def home(request):
post = Post.objects.filter(active=True)
context = {'posts':post}
return render(request,'base/index.html',context)
change your view and you are good to go
or change your html code it's totaly upto you
add this in your mainprojects urls.py
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATICFILES_DIRS)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
try this
<embed
src="{{post.file.url}}"
type="application/pdf"
frameBorder="0"
scrolling="auto"
height="500px"
width="100%"
></embed>
edit media root
MEDIA_ROOT = os.path.join(BASE_DIR,'static/images')
this is wrong, you can't have media root in static root
remove static/ and try it again
I am learning django, I would like to read a json file but I do not kown where to put it cause I cannot display.
I have place the file in the folder "static_project" but it is not working
Here is the structure of my project:
src=>templates=> Main=> home.html
src=>Myproject=> settings.py
src=>Myproject=>urls.py
here the html:
{% load static %}
<lottie-player
src= src="{% static "./myfile.json" %}" background="transparent" speed="1" style="width: 700px; height: 700px;" loop autoplay >
</lottie-player>
</div>
Hereis the main urls.py
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here is my setting :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static_project")
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
#every time a user upload folder, it will be inside the static_cdn folder and the root "media root"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
Thanks for your help
My media which i have uploaded using the form in this question View does not seem to be saving all the information in my model is being uploaded to the correct /media/images folder that i have defined. However, on access it returns a 404.
i have tried to follow the instructions on https://stackoverflow.com/a/39359264 but still am getting a 404.
my template code
{% for books in object_list %}
<img src="{{ books.cover_pic.url }}">
{% endfor %}
settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
my urls.py
urlpatterns =[
path('', views.index, name='index'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my model code for the image is
cover_pic = models.FileField(null=True, blank=True, upload_to='images/')
my media folder is at base dir (next to manage.py).
What am i overlooking?
Try this. It works for me this way. You also try some method that I commented in the models.py incase
of anything.
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = (
BASE_DIR
)
MEDIA_URL = '/media/'
models.py ...
#Here you can try changing FileField to ImageField if you like.
# You can also try directory uploads as image without backslash
#cover_pic = models.ImageField(upload_to='images')
cover_pic = models.FileField(upload_to='images/')
urls.py(project's)
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
template (.html)
{% for books in object_list %}
<img src="{{ books.cover_pic.url }}">
{% endfor %}
There is no media folder in the project, and i can see the media in db, but it won't load on template.
With this error:
Not Found: /DSC_0008.JPG
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('UserProfile.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
index.html looks something like this:
<div class="profile-userpic">
<img src="{{ my_details.associate_image.url }}" width="150" height="150" class="img-responsive" alt="my pic">
</div>
For development server,
urlpatterns = [
# ...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will create your media folder when DEBUG = True in your settings.
Make sure your image path {{ my_details.associate_image.url }} is correct. The media url must be like 127.0.0.1:8000/media/..filename.ext..
I can not show the picture in the html by django.
There is my project
This my project setting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
urls
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns=[
path('',views.main_view,name='main_view'),
]
urlpatterns += staticfiles_urlpatterns()
main.html
<img src="{% static '/my/1.jpg' %}" alt="My image">
There is my result.As you can see,the picture is desapper
setting.py
MEDIA_URL = '/pic/'
MEDIA_ROOT = os.path.join(BASE_DIR,'pic/')
urls.py
urlpatterns+=static('/pic/', document_root=settings.MEDIA_ROOT)
main.html
<img src="pic/1.jpg" alt="My image">
you just should to remove slash, because, in the settings STATIC_URL already has it.
<img src="{% static 'my/1.jpg' %}" alt="My image">
<!-- ^^^ -->
and by docs check DEBUG settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
This helper function will only work if DEBUG is True
and try to use standard static instead of staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)