1👍
✅
First, string up your
Remember to edit your
The easiest way to do this is a simple ListView
from Django’s generic class-based views.
First, string up your views.py
:
from django.views.generic import ListView
class ProductListView(ListView):
model = Product
template_name = 'product/list_view.html' # Edit this to whatever your template is.
Remember to edit your urls.py
:
from .views import ProductListView
urlpatterns = patterns('',
...
url(r'^product/$', ProductListView.as_view(), name='list'), # Edit url path and name as desired
...
)
Then, make your template:
<div class="jumbotron">
<ul>
{% for product in products %}
<li>{{ product.name }}: {{ product.description }} <img src="{{ product.image.url }}" >
{% endfor %}
</ul>
</div>
This is a very basic template which you’ll obviously want to customize. For each Product
in your database, it will display the name, description, and the image. You can customize your fields as you desire.
Other potential issues:
1) Be sure you provide the correct path to your template in your ListView
.
2) Set up your MEDIA_URL
and other media settings to allow your Product
image to display.
3) Look at other customization options in ListView
(see documentation here.)
👤Alex
Source:stackexchange.com