1๐
โ
Below code should work fine.
{% for data in filter %}
<li>{{ data.user.username }} - {{ data.user.get_full_name }}</li>
{% endfor %}
{% for Data in filter %}
<tr>
<td>{{ Data.user }}</td>
<td>{{ Data.Date }}</td>
<td>{{ Data.Status }}</td>
</tr>
{% empty %}
As you would not be using filters, so filters.py should be removed.
This will also not work. Make the changes as required.
{{ filter.form.as_table }}
๐คRyuCoder
3๐
There could be better ways to do this. As you are still learning below code does the trick.
@login_required
def search(request, *args, **kwargs):
status_list = Data.objects.get(user=request.user)
month = request.GET.get("month", None)
year = request.GET.get("year", None)
if month:
status_list = status_list.filter(Date__month=month)
if year:
status_list = status_list.filter(Date__year=year)
return render(request, 'users/data.html', {'filter': status_list})
๐คRyuCoder
2๐
You can simplify your code, django provides many helpers function to help to programmer.
in your views.py you can do:
@login_required
def search(request):
status_list = Data.objects.all()
status_filter = status_list.filter(user=request.user) //get current user id
return render(request, 'users/data.html', {'filter': status_filter})
๐คRicardo Prado
- [Django]-Django admin download data as csv
- [Django]-How to unregister table from django admin whose created when package are installed
- [Django]-How to correctly load data from a table to a form?
- [Django]-Validation Error with Multiple File Uploads in Django via Ajax
2๐
status_list = Data.objects.get(user=self.request.user)
is wrong as self is only used in class based views. No wonder it did not work.
Please try with the below code instead
status_list = Data.objects.filter(user=request.user) or
status_list = Data.objects.get(user=request.user)
So, the final code will be like
@login_required
def search(request):
status_list = Data.objects.get(user=request.user)
render(request, 'users/data.html', {'filter': status_list})
๐คRyuCoder
- [Django]-Is there a way to update the database with the changes in my models?
- [Django]-Django cors headers and server error
- [Django]-Django DB Design โ Maintaining common and historical data
Source:stackexchange.com