im getting a broken image i tried almost everything looked at a lot of questions but still not working
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static_root")
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), "static", "static_files")
]
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('products/', views.all, name='products')
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the html file
{% for item in product.productimage_set.all%}
{% if item.featured %}
<div><img src="{{ MEDIA_URL }}{{ item.image }}"/></div>
{% endif %}
Related
I set my DEBUG variable to False in setting.py and deployed my project in cpanel,used collectstatic command but static files not loading.
setting.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
if DEBUG:
STATICFILES_DIRS = [(
BASE_DIR / 'static/')
]
MEDIA_ROOT = os.path.join(BASE_DIR, "static_cdn", "media_root")
else:
STATIC_ROOT = "/home/smartsbi/public_html/static"
MEDIA_ROOT = "/home/smartsbi/public_html/media"
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
path('blog/', include('blog.urls')),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('', include('contact_us.urls')),
path('', include('service.urls')),
]
if settings.DEBUG:
# add root static files
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# add media static files
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here is my project structure
I'm trying to display the image from the static folder but it can't be displayed.
I also tried to mention the static folder in the settings.py file as below:
Here is how I'm calling the image from the base.html file.
I'm not sure what is wrong, I tried to search but the google sources can't help.
The correct format of displaying static images in django would be this
{% load static %}
<img src="{% static 'images/heart1.png' %}">
Edit
Put the following lines in your settings.py
STATIC_ROOT =os.path.join(BASE_DIR,'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
add the following to your urls.py
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
#other urls
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You have to use the static template tag in your template
{% load static %}
{% static "images/heart1.png" %}
Django is looking for my static files in the wrong place and I dont understand why
Not Found: /account/login/style.css
[26/Feb/2021 19:27:16] "GET /account/login/style.css HTTP/1.1" 404 2434
but my file should be in /static/css/style.css
This is the code in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles')
]
I use staticfiles folder to place my static files afterwards I do py src\manage.py collectstatic to transfer my files in static folder
This is my layout of the project:
In my template I am using {% load static %} and I try to load the style.css with {% static 'css/style.css' %}
Here is the urls.py in core:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accounts.urls', namespace='accounts')),
]
if settings.DEBUG:
# test mode
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py in accounts:
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name="registration/login.html",
authentication_form=UserLoginForm), name='login'),
]
Login page is the one that I have the problem with http://127.0.0.1:8000/account/login/
Template:
DEBUG is true in settings
Do you have any idea what could be the problem?
I made a template to list documents with a download link, but instead of linking to :
http://127.0.0.1:8000/media/mydoc.csv
it links to :
http://127.0.0.1:8000/myapp/myview/media/mydoc.csv
In myapp/templates/myapp/list.html
<ul>
{% for document in documents %}
<li>{{ document.docfile.name }}</li>
{% endfor %}
</ul>
document.docfile.url does actually links to media/mydoc.csv so that I guess it has to do with the template engine.
Versions
Python : 3.7.2
Django : 2.1.7
OS : Windows 10
In myapp/models.py
class Document(models.Model):
docfile = models.FileField(upload_to='documents')
In myapp/views.py
def myview(request):
documents = Document.objects.all()
return render(request, 'myapp/list.html', {'documents': documents})
In myapp/urls.py
app_name = 'myapp'
urlpatterns = [
path('', views.index, name='index'),
path('myview/', views.myview, name='myview'),
]
In myproject/urls.py
urlpatterns = [
path('', views.index, name='index'),
path('myapp/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
In settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'
Since it is my fist Django app (and first webapp ever) I guess I missed something very basic. Do you have an idea ?
Your MEDIA_URL is wrong. You missed the starting slash.
MEDIA_URL = '/media/'
Django STATIC files does not work in Develop mode.
I tried in many ways, but I did not succeed.
Could someone tell me where I'm wrong?
Thank you very much for your attention.
STATIC_URL = '/static/'
STATIC_ROOT = "/Users/user/Projects/RMA/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
....
'django.contrib.staticfiles',
routes
urlpatterns = [
url(r'^$', views.index, name='index')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
View
def index(request):
return render(request, 'base.html', {'question': 'ads'})
template
{% load static %}
<link href="{% static 'site/bower_components/select2/dist/css/select2.min.css' %}" rel="stylesheet">