How to create a submit button with multiple options that can alter a csv file and post it back

huangapple go评论106阅读模式
英文:

How to create a submit button with multiple options that can alter a csv file and post it back

问题

我试图创建一个提交按钮,列出特定用户的文件,并为每个下拉列表添加相关操作。我可以下载CSV文件并修改它,保存为新文件,但似乎无法重新发布文件并将其添加到列表中。

我卡在如何将newFile.csv添加到DocumentForm(request.POST, request.FILES)上。

models.py

  1. class Document(models.Model):
  2. user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
  3. description = models.CharField(max_length=255, blank=False)
  4. document = models.FileField(upload_to=fileLocation)
  5. uploaded_at = models.DateTimeField(auto_now_add=True)

forms.py

  1. class DocumentForm(forms.ModelForm):
  2. class Meta:
  3. model = Document
  4. fields = ('description', 'document')

html

  1. {% for file in files %}
  2. <form id="file" action="{% url 'process' %}" method="post" enctype="multipart/form-data">
  3. {% csrf_token %}
  4. <select name="selectedOption">
  5. <option value="1">1</option>
  6. <option value="2">2</option>
  7. <option value="3">3</option>
  8. </select>
  9. <input type="submit" value="Submit" class="btn btn-sm">
  10. </form>
  11. {% endfor %}

views.py

  1. def process(request):
  2. selected_option = request.POST.get('selectedOption')
  3. form = DocumentForm(request.POST, request.FILES)
  4. current_client = request.user
  5. files = Document.objects.filter(user=current_client)
  6. fileResponse = s3_client.get_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=document)
  7. df = pd.read_csv(fileResponse.get("Body"), encoding='utf-8', dtype=str)
  8. df.to_csv('temp/temp.csv', index=False, header=True, encoding='utf-8-sig')
  9. newFile = 'temp/temp.csv'
  10. if request.method == 'POST':
  11. form = DocumentForm(request.POST, request.FILES)
  12. if form.is_valid():
  13. instance = form.save(commit=False)
  14. instance.user = request.user
  15. instance.save()
  16. return redirect('/filelist')
  17. else:
  18. form = DocumentForm()
  19. return render(request, 'accounts/filelist.html', {'selected_option': selected_option, 'form': form, 'files': files})

How to create a submit button with multiple options that can alter a csv file and post it back

  1. 请注意,这是您的代码的翻译部分,不包括代码块中的具体细节和操作。
  2. <details>
  3. <summary>英文:</summary>
  4. I&#39;m trying to create a submit button that lists files for a particular user that has actions associated to each drop down. I can download the csv file and alter it, save it as a new file but I can not seem to repost the file and add it to the list.
  5. I am stuck how to add the newFile.csv to the DocumentForm(request.POST, request.FILES)
  6. models.py
  7. class Document(models.Model):
  8. user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
  9. description = models.CharField(max_length=255, blank=False)
  10. document = models.FileField(upload_to=fileLocation)
  11. uploaded_at = models.DateTimeField(auto_now_add=True)
  12. forms.py
  13. class DocumentForm(forms.ModelForm):
  14. class Meta:
  15. model = Document
  16. fields = (&#39;description&#39;, &#39;document&#39;)
  17. html
  18. {% for file in files %}
  19. &lt;form id=&quot;file&quot; action= &quot;{% url &#39;process&#39; %}&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
  20. {% csrf_token %}
  21. &lt;select name=&quot;selectedOption&quot;&gt;
  22. &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
  23. &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
  24. &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
  25. &lt;/select&gt;
  26. &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;btn btn-sm&quot;&gt;
  27. &lt;/form&gt;
  28. {% endfor %}
  29. views.py
  30. def process(request):
  31. selected_option = request.POST.get(&#39;selectedOption&#39;)
  32. form = DocumentForm(request.POST, request.FILES)
  33. current_client = request.user
  34. files = Document.objects.filter(user=current_client)
  35. fileResponse = s3_client.get_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=document)
  36. df = pd.read_csv(fileResponse.get(&quot;Body&quot;), encoding=&#39;utf-8&#39;, dtype=str)
  37. df.to_csv(&#39;temp/temp.csv&#39;, index=False, header=True, encoding=&#39;utf-8-sig&#39;)
  38. newFile = &#39;temp/temp.csv&#39;
  39. if request.method == &#39;POST&#39;:
  40. form = DocumentForm(request.POST, request.FILES)
  41. if form.is_valid():
  42. instance = form.save(commit=False)
  43. instance.user = request.user
  44. instance.save()
  45. return redirect(&#39;/filelist&#39;)
  46. else:
  47. form = DocumentForm()
  48. return render(request, &#39;accounts/filelist.html&#39;, {&#39;selected_option&#39;:selected_option, &#39;form&#39;: form, &#39;files&#39;: files})
  49. [![enter image description here][1]][1]
  50. [1]: https://i.stack.imgur.com/PX51M.png
  51. </details>
  52. # 答案1
  53. **得分**: 0
  54. 更改了views.py中的post如下。
  55. ```python
  56. if request.method == 'POST':
  57. file_path = newFile
  58. with open(file_path, 'rb') as file:
  59. document_instance = Document(
  60. user_id=request.user.id,
  61. description="新文件"
  62. )
  63. document_instance.document.save(os.path.basename(file_path), file)
  64. document_instance.save()
英文:

changed the views.py post to the below.

  1. if request.method == &#39;POST&#39;:
  2. file_path = newFile
  3. with open(file_path, &#39;rb&#39;) as file:
  4. document_instance = Document(
  5. user_id = request.user.id,
  6. description = &quot;new file&quot;
  7. )
  8. document_instance.document.save(os.path.basename(file_path), file)
  9. document_instance.save()

huangapple
  • 本文由 发表于 2023年7月28日 02:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定