*{% load 'static' %} *use this and make sure that* image src* path in *jinja
tag* with *static* keyword
*e.g *
*<img src="{% static ' path-to-image ' %}" />*
B. Raina
On Wed, 18 Sep 2019 at 01:00, 'Amitesh Sahay' via Django users <
[email protected]> wrote:
> Below is the snippet of my project tree::
>
> [image: Inline image]
>
>
> Regards,
> Amitesh Sahay
> *91-750 797 8619*
>
>
> On Wednesday, 18 September, 2019, 12:58:57 am IST, Amitesh Sahay <
> [email protected]> wrote:
>
>
> Hello Users,
>
> I am building an app called "SHOP". For some very wired reason I am
> getting below error:
> ------------------------------------------------------------
> ValueError at /shop/
>
> The 'image' attribute has no file associated with it.
>
> Request Method: GET
> Request URL: http://localhost:8000/shop/
> Django Version: 2.2.5
> Exception Type: ValueError
> Exception Value:
>
> The 'image' attribute has no file associated with it.
>
> Exception Location:
> /home/amitesh/PycharmProjects/myvenv/lib/python3.6/site-packages/django/db/models/fields/files.py
> in _require_file, line 38
> Python Executable: /home/amitesh/PycharmProjects/myvenv/bin/python
> Python Version: 3.6.8
> Python Path:
>
> ['/home/amitesh/PycharmProjects/perfectcushion',
> '/usr/lib/python36.zip',
> '/usr/lib/python3.6',
> '/usr/lib/python3.6/lib-dynload',
> '/home/amitesh/PycharmProjects/myvenv/lib/python3.6/site-packages']
>
> Server time: Tue, 17 Sep 2019 18:59:22 +0000
> --------------------------------------------------------------------
> When I see the debug page further I see that my base.html is complaining,
> below is the error:
> Error during template rendering
>
> In template
> /home/amitesh/PycharmProjects/perfectcushion/SHOP/templates/base.html,
> error at line *0*
> The 'image' attribute has no file associated with it.
> 1 {% load staticfiles %}
> 2 <!DOCTYPE html>
> 3 <html lang="en">
> 4 <head>
> 5 <meta charset="UTF-8">
> 6 <meta name="description", content="{% block metadescription %}{%
> endblock %}">
> 7 <title>{% block title %}{% endblock %}</title>
> 8 </head>
> 9 <body>
>
>
> base.html
>
> {% load staticfiles %}
> <!DOCTYPE html>
> <html lang="en">
> <head>
> <meta charset="UTF-8">
> <meta name="description", content="{% block metadescription %}{% endblock
> %}">
> <title>{% block title %}{% endblock %}</title>
> </head>
> <body>
> <div>
> {% include 'header.html' %}
> {% include 'navbar.html' %}
> {% block content %}
> {% endblock %}
> </div>
> {% include 'footer.html' %}
>
> </body>
> </html>
>
> headers.html
>
> {% load staticfiles %}
> <header>
> <center>
> <img src="{% static 'img/logo.png' %}" alt="Perfect Cushion Store">
> </center>
> </header>
>
> navbar.html
>
> {% load staticfiles %}
> <nav>
> <ul>
> <li><a href="{% url 'shop:allProdCat' %}">All Products</a></li>
> <li>Your Cart()</li>
> </ul>
> </nav>
>
> category.html
>
> {% extends 'base.html' %}
> {% load staticfiles %}
> {% block metadescription %}
> {% if category %}
> {{ category.description|truncatewords:125 }}
> {% else %}
> Welcome to the cushion store where you can buy comfy and awesome
> cushions.
> {% endif %}
> {% endblock %}
>
> {% block title %}
> {% if category %}
> {{ category.name }} - Perfect Cushion Store
> {% else %}
> See our Cushion Collection - Perfect Cushion Store
> {% endif %}
> {% endblock %}
>
> {% block content %}
> <!-- Breadcrumb navigation-->
> {% if category %}
> <div>
> <div>
> <p><a href="{% url 'SHOP:allProdCat' %}">Our Product
> Collection</a> | {{ category.name }}</p>
> </div>
> </div>
> {% endif %}
> <div>
> {% if category %}
> <img src="{{ category.image.url }}" alt="{{ category.name }}">
>
> </div>
> <br>
> <div>
> <h1>{{ category.name }}</h1>
> <p>{{ category.description }}</p>
> </div>
> {% else %}
> <img src="{% static 'img/banner.jpg' %}" alt="Our Products
> Collection">
> </div>
> <br>
> <div>
> <h1>Our Products collection</h1>
> <p>
> Lorem Ipsum is simply dummy text of the printing and typesetting
> industry. Lorem Ipsum has been the industry's standard dummy text
> ever since the 1500s, when an unknown printer took a galley of
> type and.</p>
> </div>
> {% endif %}
> <div>
> <div>
> {% for product in products %}
> <div>
> <div>
> <a href=""><img src="{{ product.image.url }}" alt="{{
> product.name }}"></a>
> <div>
> <h4>{{ product.name }}</h4>
> <p>{{ product.price }}</p>
> </div>
> </div>
> </div>
> {% endfor %}
> </div>
> </div>
> {% endblock %}
>
> SHOP/urls.py
>
> from django.urls import path
> from . import views
>
> app_name = 'shop'
>
> urlpatterns = [
> path('', views.allProdCat, name='allProdCat'),
> path('<slug:c_slug>/', views.allProdCat, name='products_by_category'),
> ]
>
> project/urls.py
>
> from django.contrib import admin
> from django.urls import path, include
> from SHOP import views
> from django.conf import settings
> from django.conf.urls.static import static
>
> urlpatterns = [
> path('admin/', admin.site.urls),
> path('shop/', include('SHOP.urls')),
> ]
>
> # We need to map the static and the media URLS with the below settings
> if settings.DEBUG:
> urlpatterns += static(settings.STATIC_URL,
> document_root=settings.STATIC_ROOT)
> urlpatterns += static(settings.MEDIA_URL,
> document_root=settings.MEDIA_ROOT)
>
> views.py
>
> from django.shortcuts import render, get_object_or_404
> from .models import Category, Product
>
>
> def allProdCat(request, c_slug=None):
> c_page = None
> products = None
> if c_slug is not None:
> c_page = get_object_or_404(Category, c_slug=c_slug)
> products = Product.objects.filter(category=c_page, available=True)
> else:
> products = Product.objects.all().filter(available=True)
> return render(request, 'shop/category.html', {'category': c_page,
> 'products': products})
>
> Please help me figure out my mistake.
>
>
> Regards,
> Amitesh Sahay
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1020759912.6891903.1568748605693%40mail.yahoo.com
> <https://groups.google.com/d/msgid/django-users/1020759912.6891903.1568748605693%40mail.yahoo.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CA%2BMA24Y4iGckKeP4F-Szamqnj-0pybomp7zOinPfkNxLX%2Bs%2BBA%40mail.gmail.com.