0

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>
2
  • I don't fully understand what you're trying to do, but if you have queryset_list = fp.objects.all() as the last line of that if query:, the previous line is pointless. queryset_list will always have all the fp objects. You are also returning from within the for loop, so only the first iteration will run, after skipping the header. Commented Apr 9, 2018 at 18:46
  • hmm good point, I need to remove "queryset_list= fp.objects.all()" What I try to do actually print all within the csv file to html file and if what I printed is already in my database retrieve and print next to csv file retrieved value. Commented Apr 9, 2018 at 19:02

1 Answer 1

1

Instead of this below part,

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)

you need to modify as per below

queryset_list = FP.objects.filter(FP_Item = query)
            context = {'lines': lines,
                       'instances': queryset_list,
                       }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.