'how to assign an id to an item when clicking on a link beside it in Django?
I'm trying to assign an id to a CSV file's name so that when I click on a link beside I can access the CSV file itself on another web page to view it.
but I keep getting this error 'QueryDict' object has no attribute 'objects'
Note: the CSV files are taken from a form and saved in a file on the PC, I'm trying to retrieve the file from the PC itself.
This is the views.py
def read_datasets(request):
file = request.POST.objects.get(id=id)
# print(file)
path = r"C:/Users/user/Desktop/Fault Detection App/Uploaded_Datasets/"
# csv_file = request.FILES['file2']
# csv_file = request.POST.get('file2')
path1, dirs, files = next(os.walk(path))
file_count = len(files)
print(file_count)
# dataframes_list_html = []
file_names = []
# file_name = csv_file.name
for i in range(file_count):
temp_df = pd.read_csv(path+files[i])
print(files[i])
# dataframes_list_html.append(temp_df.to_html(index=False))
file_names.append(files[i])
# print(dataframes_list_html)
return render(request,'blog/view_datasets.html',{'names': file_names})
def one_dataset(request):
path = r"C:/Users/user/Desktop/Fault Detection App/Uploaded_Datasets/"
path1, dirs, files = next(os.walk(path))
file_count = len(files)
print(file_count)
dataframes_list_html = []
for i in range(file_count):
temp_df = pd.read_csv(path+files[i])
print(files[i])
dataframes_list_html.append(temp_df.to_html(index=False))
return render(request, 'blog/single_dataset.html', {'dataframes':dataframes_list_html})
and the HTML File
<body>
{% for dataframe in dataframes %}
<div id="customers">
{{ dataframe|safe }}
<hr>
</div>
{% endfor %}
</body>
Solution 1:[1]
The request.POST
is a dictionary. So you have to remove .objects
def read_datasets(request):
file = request.POST.get("...")
...
Solution 2:[2]
use this piece of code
import pandas as pd
col_1 = your_list[0:89]
col_2 = your_list[90:179]
col_3 = your_list[180:269]
my_dict = {
'Col A': col_1,
'Col B': col_2,
'Col C': col_3
}
df = pd.DataFrame(my_dict)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sunderam Dubey |
Solution 2 | Ali Jalili marand |