Django:如何通过views.py更改值
Posted
技术标签:
【中文标题】Django:如何通过views.py更改值【英文标题】:Django: How can I changes values via views.py 【发布时间】:2021-09-18 23:25:16 【问题描述】:我正在尝试制作一个“上传”页面,以便我可以使用 pd.to_sql() 将文件和进程上传到 postgres DB。所有数据已成功记录到数据库中,但无法更改“is_recorded”和“has_issue”的值。这是我目前正在处理的文件。
在上传/models.py中
from django.db import models
class Upload(models.Model):
file_name = models.FileField(upload_to='uploads', max_length=255)
uploaded = models.DateTimeField(auto_now_add=True)
# When uploaded successfully
is_recorded = models.BooleanField(default=False)
# When it has a problem to fix
has_issue = models.BooleanField(default=False)
def __str__(self):
return f'self.file_name: self.id'
在views.py中
import os
from django.shortcuts import render
from core.db_implements import upload_csv_to_DB
from upload.forms import UploadForm
from upload.models import Upload
def upload_view(request):
error_message = None
success_message = None
form = UploadForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
form = UploadForm()
try:
obj = Upload.objects.get(is_recorded=False)
recorded, issue, success_message, error_message = ***upload_csv_to_DB***(obj)
obj.is_recorded = recorded
obj.has_issue = issue
obj.save()
success_message = success_message
error_message = error_message
except:
error_message = 'Something went wrong.'
context=
'form': form,
'success_message': success_message,
'error_message': error_message,
return render(request, 'upload/upload.html', context)
在forms.py中
from django import forms
from django.db.models import fields
from django.forms.fields import FileField
from django.forms.models import ModelFormMetaclass
from .models import Upload
class UploadForm(forms.ModelForm):
# file_name = forms.FileField(max_length=255, label=False)
class Meta:
model=Upload
fields = ('file_name',)
widgets =
'file_name': forms.FileInput(
attrs=
'class': 'form-control',
)
我将获取文件对象并转换为数据帧并记录到数据库中的函数 (upload_csv_to_DB) 外包。如果成功,recorded=True 应该返回 sucess_message,但是,如果出现问题,issue=True 应该返回 error_message。无论如何,记录和问题总是错误的,并且有来自除子句“出错了。”的 error_message。请给我建议...
【问题讨论】:
【参考方案1】:我找到了原因。我没有初始化外包函数的返回值upload_csv_to_DB
。将返回的值初始化为之后,一切都按预期工作。这是解决问题的代码。
在upload_csv_to_DB.py
:
def upload_csv_to_DB(csv_obj):
recorded = False
issue = False
success_message = None
error_message = None
upload_file = csv_obj.file_name.path
...
return (recorded, issue, success_message, error_message)
在views.py
:
def upload_view(request):
error_message = None
success_message = None
recorded =False
issue = False
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
try:
obj = Upload.objects.get(is_recorded=False)
recorded, issue, success_message, error_message = upload_csv_to_DB(obj)
obj.is_recorded = recorded
obj.has_issue = issue
obj.save()
success_message = success_message
error_message = error_message
except:
error_message = 'Something went wrong.'
else:
form = UploadForm()
context=
'form': form,
'success_message': success_message,
'error_message': error_message,
return render(request, 'upload/upload.html', context)
【讨论】:
以上是关于Django:如何通过views.py更改值的主要内容,如果未能解决你的问题,请参考以下文章
Django如何通过单击带有变量的提交按钮从ajax调用views.py中的函数
当socket server和views.py分成两个文件时,如何通过Django视图发送套接字消息?
当 Django views.py 没有将字典值传递给 html 时如何解决?