I have a django model and csv file separately. What I want to do is that user upload a csv file with a single colomn ('fp_Item').If colomn line exists in the django model as per below
(queryset_list.filter(
Q(fp_Item__contains=query)))
I want to retrieve the necessary fields from the database and show within html if does not exists it shouldn't retrieve anything and leave empty but still should have to print the csv file line.
def check_fp(request):
if not request.user.is_active:
return render(request, 'login.html')
else:
if request.method == 'POST' and request.FILES['csv_file2']:
myfile = request.FILES['csv_file2']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
data = csv.reader(fs.open(filename, mode='r'))
queryset_list = fp.objects.all()
lines=[]
for row in data:
if row[0] != 'fp_Item':
line = row[0]
lines.append(line)
query= line
if query:
queryset_list.filter(
Q(fp_Item__contains=query))
queryset_list= fp.objects.all()
context = {'lines': lines,
'instances': queryset_list,
}
return render(request, 'check_fp.html', context)
context = {'lines': lines,
'instances': queryset_list,
}
return render(request, 'check_fp.html', context)
return render(request, 'check_fp.html', {})
lines.append(line) is working and writing csv column to html file but I couldn't somehow bind the django model and the csv column together. Isn't "if query" method usable for this scenario ?
Here is my html file: I want to match line at retrieve description and detail,timestamp and updated fields from the database. Am I looping false here ?
<tbody>
{% for line in lines %}
{% for instance in instances %}
<tr>
<td width="25%>
<a href="#"> {{ line }} </a>
</td></tr>
<td>
{{ instance.description }}
</td>
<td>
{{ instance.detail }}
</td>
<td width="180">
{{ instance.timestamp }}
</td>
<td width="180">
{{ instance.updated }}
</td>
<td width="200">
</td>
</tr>{% endfor %} {% endfor %}
</tbody>
</table>
queryset_list = fp.objects.all()as the last line of thatif query:, the previous line is pointless.queryset_listwill always have all thefpobjects. You are also returning from within theforloop, so only the first iteration will run, after skipping the header.