no thumbnail only image path is showing in django template - django-views

----------------------- root 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('estate.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
----------------------- app urls.py ------------------------------
from django.urls import path
from . import views
from .views import index, about, category, PostListView, PostDetailView
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('category/', views.category, name='category'),
path('liste/', PostListView.as_view(), name='liste'),
path('detail/<int:id>/', PostDetailView.as_view(), name='detail-list'),
----------------------- models.py ----------------------
class lists(models.Model):
...
photos = models.ImageField(upload_to='photos/')
...
def __str__ (self):
return self.title
----------------------- views.py -----------------------
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from .models import *
def index(request):
listings = lists.objects.filter(is_active=True)
hit10 = lists.filter().order_by('-hit_counter')[:10]
context = {
'listings':listings,
'hit10':hit10,
}
return render(request, 'index.html', context)
class PostListView(ListView):
queryset = lists.objects.filter(is_active=True)
paginate_by = 5
class PostDetailView(DetailView):
def get_object(self):
id_=self.kwargs.get("id")
return get_object_or_404(listings, id=id_)
----------------------- template.py -----------------------
{% extends 'base.html'%}
{% load static %}
{% block content %}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Photo</th>
<th scope="col">Title</th>
<th scope="col">Date</th>
</tr>
</thead>
{% for obj in object_list %}
{% if obj.is_active %}
<tbody>
<tr>
<th scope="row">{{ obj.id }}</th>
<td>{{ obj.photo.url }}</td>
<td>{{ obj.title }}</td>
<td>{{ obj.date }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
{% endblock content %}
THERE IS NO THUMBNAIL SHOWING THE RESULT FOR IMAGE IS LIKE THAT : /media/photo/01.jpg
everythink else is fine. Thank you.

i found the reason;
in template.py
<tr>
<th scope="row">{{ obj.id }}</th>
THIS CODE SHOULD BE ---> <td>{{ obj.photo.url }}</td>
<td>{{ obj.title }}</td>
<td>{{ obj.date }}</td>
</tr>
<tr>
<th scope="row">{{ obj.id }}</th>
LIKE THIS ONE ---> <td><img style="width:50%;" class="img-thumbnail" src="{{obj.photo.url}}" alt="IMG"></td>
<td>{{ obj.title }}</td>
<td>{{ obj.date }}</td>
</tr>
thanks to everyone who was helping.

Related

{{ obj.photos.url }} not showing images

----------------------- root 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('estate.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
----------------------- app urls.py ------------------------------
from django.urls import path
from . import views
from .views import index, about, category, PostListView, PostDetailView
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('category/', views.category, name='category'),
path('liste/', PostListView.as_view(), name='liste'),
path('detail/<int:id>/', PostDetailView.as_view(), name='detail-list'),
----------------------- models.py ----------------------
class lists(models.Model):
...
photos = models.ImageField(upload_to='photos/')
...
def __str__ (self):
return self.title
----------------------- views.py -----------------------
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from .models import *
def index(request):
listings = lists.objects.filter(is_active=True)
hit10 = lists.filter().order_by('-hit_counter')[:10]
context = {
'listings':listings,
'hit10':hit10,
}
return render(request, 'index.html', context)
class PostListView(ListView):
queryset = lists.objects.filter(is_active=True)
paginate_by = 5
class PostDetailView(DetailView):
def get_object(self):
id_=self.kwargs.get("id")
return get_object_or_404(listings, id=id_)
----------------------- template.py -----------------------
{% extends 'base.html'%}
{% load static %}
{% block content %}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Photo</th>
<th scope="col">Title</th>
<th scope="col">Date</th>
</tr>
</thead>
{% for obj in object_list %}
{% if obj.is_active %}
<tbody>
<tr>
<th scope="row">{{ obj.id }}</th>
<td>{{ obj.photo.url }}</td>
<td>{{ obj.title }}</td>
<td>{{ obj.date }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
{% endblock content %}
THERE IS NO THUMBNAIL SHOWING THE RESULT FOR IMAGE IS LIKE THAT : /media/photo/01.jpg
everythink else is fine. Thank you.
The image won't show because you didn't put the url in a image html tag.
Modified the template.html file:
...
<td> <img src=/static/{{ obj.photo.url }}></td>
...
Note:'static' should be your STATIC_URL which you set in settings.py.
i found the reason;
in template.py
<tr>
<th scope="row">{{ obj.id }}</th>
THIS CODE SHOULD BE ---> <td>{{ obj.photo.url }}</td>
<td>{{ obj.title }}</td>
<td>{{ obj.date }}</td>
</tr>
<tr>
<th scope="row">{{ obj.id }}</th>
LIKE THIS ONE ---> <td><img style="width:50%;" class="img-thumbnail" src="{{obj.photo.url}}" alt="IMG"></td>
<td>{{ obj.title }}</td>
<td>{{ obj.date }}</td>
</tr>
thanks to everyone who was helping especially to #Adil Shirinov.

django checkbox get checkbox value from html

html file
<form method="post">
{% csrf_token %}
<table border="2" bordercolor="black" width="500" height="100" cellspacing="0" cellpadding="5">
<thead>
<tr>
<th>selection</th>
<th>student name</th>
<th>e-mail</th>
<th>CV</th>
</tr>
</thead>
<tbody>
{% for ta in tas %}
<tr>
<td><input type="checkbox" name="checkbox_list" value="{{ ta.id }}"></td>
<td>{{ ta.user.first_name }} {{ ta.user.last_name }}</td>
<td>{{ ta.user.email }}</td>
<td>view cv</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit">next</button>
</form>
views.py
def ta_ranking(request, id):
print('hello word!')
check_box_list = request.POST.getlist('checkbox_list')
if check_box_list:
print(check_box_list)
potential_ta = TA.objects.filter(id__in=check_box_list)
return render(request, 'ta_ranking.html', {'potential_ta': potential_ta, 'course_id': id})
return redirect('ta_list', id=id)
my question is that the ta_ranking class in views.py didn't be called when I click the submit button in html. How can I fix this problem?
Thanks for any help!
You didn't include the path to the view in your form:
<form method="POST" action="/path/to/ta_ranking">
...
</form>
You also need to add it to your urls.py if you haven't already:
from django.urls import path
from myapp.views import ta_ranking
urlpatterns = [
path('/path/to/ta_ranking', ta_ranking),
]

How to solve __init__() takes 1 positional argument but 2 were given

views.py
from django.views.generic import ListView
from django.contrib.auth.models import User
class UserListView(ListView):
model = User
template_name = 'infinitescroll/articles.html'
context_object_name = 'users'
paginate_by = 10
queryset = User.objects.all()
urls.py
from django.contrib import admin
from django.urls import path
from infinitescroll.views import UserListView
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',UserListView, name='home'),
]
articles.html
<table class="table table-bordered">
<thead>
<tr>
<th>Username</th>
<th>First name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.username }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I don't know what causes an error it gives error locations in Exception
Location: C:\xampp\htdocs\8moviesdb\infinite\pal\lib\site-packages\django\core\handlers\base.py
in _get_response, line 113
In urls.py, instead of just UserListView, use UserListView.as_view(). It's how Django's class based views work.

URL won't redirect to detailview pk Django

I have a simple project and made troubled fixing the redirection.
The url is not redirecting to my statistics_detail.html. whenever i click it just adds the pk on the link but won't redirects
here is my url.py:
urlpatterns = [
url('^$', views.index, name='index'),
re_path(r'^(?P<pk>\d+)/$', views.StatisticsDetailView.as_view(), name='detail'),
url('blist/', views.StatisticListView.as_view(), name='blist'),
url('user_list', DisplayView.as_view(), name='user_list'),
url('new_user', views.new_user, name='new_user'),
url('push_user_tb', views.push_user_tb, name='push_user_tb'),
url('push_user_prod', views.push_user_prod, name='push_user_prod'),
url('st', views.display_stats_logs, name='st'),
url('today', views.display_today, name='today'),
url('balance', views.display_balance, name='balance'),
]
views.py
class StatisticsDetailView(DetailView):
context_object_name = 'statistics_details'
model = models.Statistics
template_name = 'provision/statistics_detail
here is also statistics_detail.html:
{% extends 'base.html' %}
{% block content %}
<p>Statistics</p>
<div class="container">
<table class="table">
<tr>
<th>Name</th>
<th>Mac ID</th>
<th>Hours</th>
<th>Date</th>
<th>Status</th>
</tr>
{% for clients in object_list %}
<tr>
<td>{{ clients.name }}</td>
<td>{{ clients.mac_add }}</td>
<td>{{ clients.minutes_used|cut:".0" }}</td>
<td>{{ clients.date }}</td>
<td>{{ clients.status }}</td>
</tr>
{% endfor %}
</table>
It is {% now "jS F Y H:i" %}
</div>
{% endblock %}
As you can see on the screenshot below. nothing happens if i click the clients.status which supposedly redirects to statistics_detail.html
Browser URL: http://127.0.0.1:8000/prov/blist/
After Click the status it would only add http://127.0.0.1:8000/prov/blist/2146/ but doesn't work
<a href="{% url 'detail' clients.id %}">
You need a leading slash: <a href="/{{clients.id}}/">
Even better, use the {% url %} tag rather than outputting the URL manually.

Displaying a Table in Django from Database

How do you display the information from a database table in a table format on a webpage? Is there a simple way to do this in django or does it require a more complicated approach. More specifically, how do you pretty much port over the columns and rows in a database table to a visual table that can be seen from a url?
The easiest way is to use a for loop template tag.
Given the view:
def MyView(request):
...
query_results = YourModel.objects.all()
...
#return a response to your template and add query_results to the context
You can add a snippet like this your template...
<table>
<tr>
<th>Field 1</th>
...
<th>Field N</th>
</tr>
{% for item in query_results %}
<tr>
<td>{{ item.field1 }}</td>
...
<td>{{ item.fieldN }}</td>
</tr>
{% endfor %}
</table>
This is all covered in Part 3 of the Django tutorial. And here's Part 1 if you need to start there.
$ pip install django-tables2
settings.py
INSTALLED_APPS , 'django_tables2'
TEMPLATES.OPTIONS.context-processors , 'django.template.context_processors.request'
models.py
class hotel(models.Model):
name = models.CharField(max_length=20)
views.py
from django.shortcuts import render
def people(request):
istekler = hotel.objects.all()
return render(request, 'list.html', locals())
list.html
{# yonetim/templates/list.html #}
{% load render_table from django_tables2 %}
{% load static %}
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="{% static
'ticket/static/css/screen.css' %}" />
</head>
<body>
{% render_table istekler %}
</body>
</html>
If you want to table do following steps:-
views.py:
def view_info(request):
objs=Model_name.objects.all()
............
return render(request,'template_name',{'objs':obj})
.html page
{% for item in objs %}
<tr>
<td>{{ item.field1 }}</td>
<td>{{ item.field2 }}</td>
<td>{{ item.field3 }}</td>
<td>{{ item.field4 }}</td>
</tr>
{% endfor %}
The answers in this thread rely on manually feeding column names, and I prefer to have some way of viewing a Django model completely by default. I have cooked up the solution below:
views.py
from django.shortcuts import render
from .models import TABLEOFINTEREST
def TABLEOFINTEREST(request):
MODEL_HEADERS=[f.name for f in TABLEOFINTEREST._meta.get_fields()]
query_results = [list(i.values()) for i in list(TABLEOFINTEREST.objects.all().values())]
#return a response to your template and add query_results to the context
return render(request, "/TABLEOFINTEREST.html", {
"query_results" : query_results,
"model_headers" : MODEL_HEADERS
})
TABLEOFINTEREST.html:
<table>
<tr>
{% for item in model_headers %}
<th>{{ item }}</th>
{% endfor %}
</tr>
{% for all_rows in query_results %}
<tr>
{% for every_column in all_rows %}
<td>{{ every_column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("TABLEOFINTEREST", views.TABLEOFINTEREST, name="TABLEOFINTEREST")
]
Tested and validated on my machine with multiple tables.