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

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.

Related

I cannot load the image in my template, I tried changing all upload file location and everything else and its the same with category field

models.py
class product(models.Model):
categoryChoices=[('food','Food'),('clothing','Clothing'),('books','Books'),('furniture','Furniture'),('others','Others')]
product_name=models.CharField(max_length=40)
product_decsription=models.CharField(max_length=100)
pub_date=models.DateTimeField(auto_now=True)
image=models.ImageField(upload_to='CharApp/images/',default='')
address=models.CharField(max_length=200, default='')
city= models.CharField(max_length=40)
station= models.CharField(max_length=40)
category= models.CharField(choices=categoryChoices,max_length=30)
Views.py
def index(request):
donations=product.objects.all()
return render(request,'CharApp/index.html',{'donations':donations})
Urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.start,name='start'),
path('home/',views.index,name='index'),
path('upload/',views.productFormView),
path('about/',views.about,name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Forms.py
class productForm(forms.ModelForm):
class Meta:
model=product
fields='__all__'
widgets= {
'product_name': forms.TextInput(attrs={'class':'form-control'}),
'product_decsription': forms.TextInput(attrs={'class':'form-control'}),
'pub_date': forms.DateInput(attrs={'class':'form-control'}),
'image': forms.FileInput(attrs={'class':'form-control'}),
'address': forms.TextInput(attrs={'class':'form-control'}),
'city': forms.TextInput(attrs={'class':'form-control'}),
'station': forms.TextInput(attrs={'class':'form-control'}),
'category': forms.Select(attrs={'class':'form-control'}),
template
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Category</th>
<th scope="col">Station</th>
<th scope="col">Image</th>
</tr>
</thead>
{% if donations %}
<tbody>
{% for d in donations %}
<tr>
<th scope="row">1</th>
<td>{{d.product_name}}</td>
<td>{{d.product_description}}</td>
<td>{{d.category}}</td>
<td>{{d.station}}</td>
<td>
<img src="/{{ BASIC_DIR }}/{{d.image}}" width="120"/>
</td>
<td><button type="button" class="btn btn-success">Add</button></td>
</tr>
{% endfor %}
{% else %}
<p>no donations</p>
{% endif %}
I tried chnaging the img src call to a d.image.url call,and alot of other things it just doesnt load, the view just doesnt load this field even tho they are available in the database
Change your template with
{% for d in donations %}
<tr>
<th scope="row">1</th>
<td>{{d.product_name}}</td>
<td>{{d.product_description}}</td>
<td>{{d.get_category_display}}</td>
<td>{{d.station}}</td>
<td>
<img src="{{d.image.url}}" width="120"/>
</td>
<td><button type="button" class="btn btn-success">Add</button></td>
</tr>
{% endfor %}
Explanation:
For each model field that has choices set, Django will add a method to retrieve the human-readable name for the field’s current value. See get_FOO_display() in the database API documentation.
get_FOO_display
Docs: https://docs.djangoproject.com/en/4.0/ref/models/fields/#choices

Get All Users in django

I want to display all the users, in my template but I i received this error.
TypeError at /list/
init() takes 1 positional argument but 2 were given
views.py
class UsersView(TemplateView):
template_name = 'list.html'
context = super(UsersView, self).get_context_data(**kwargs)
context['object_list'] = User.objects.values()
list.html
<tbody>
<tr>
<th scope="col">Id</th>
<th scope="col">username</th>
<th scope="col">email Adress</th>
<th scope="col">First Name </th>
<th scope="col">Last Name</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }} </td>
<td>{{ user.username}}</td>
<td>{{ user.email }}</td>
<td>{{ user.first_name}}</td>
<td>{{ user.last_name }}</td>
How could you pull all users?
There is no self nor context, etc. in a class. You should override the .get_context_data(…) method [Django-doc]:
class UsersView(TemplateView):
template_name = 'list.html'
def get_context_data(self, *args, **kwargs):
context = super(UsersView, self).get_context_data(*args, **kwargs)
context['object_list'] = User.objects.all()
return context
You named your template variable ojbect_list, so you should iterate over object_list, not users:
{% for user in object_list %}
<tr>
<td>{{ user.id }} </td>
<td>{{ user.username}}</td>
<td>{{ user.email }}</td>
<td>{{ user.first_name}}</td>
<td>{{ user.last_name }}</td>
</tr>
{% endfor %}
It might however make more sense to use a ListView [Django-doc] instead:
from django.views.generic.list import ListView
class UsersView(ListView):
template_name = 'list.html'
model = User

no thumbnail only image path is showing in django template

----------------------- 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.

{{ 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),
]