python - 如何处理 'str' 对象在我的 django 项目中没有属性 '_meta' 错误
问题描述
我有一个 django 项目,它有一个表单来上传保存在我的数据库中的数据。但是有一些我无法处理的问题。我试图找到解决问题的文档,但我找不到可以帮助我的文档。
视图.py
from django.shortcuts import render,redirect
from django.contrib.auth.models import User,auth
from exam.functions.functions import handle_uploaded_file
from django.http import HttpResponse
from exam.forms import Examinationform
from exam.models import Examination
import json
# Create your views here.
def upload1(request):
if request.method == 'POST':
main=[]
exam = Examinationform(request.POST, request.FILES)
if exam.is_valid():
for _file in request.FILES.getlist('fileexam'):
_file = request.FILES['fileexam'] #devide list of files.
filename = _file.name
main.append("filename")
handle_uploaded_file(_file)
Examination.namefileexam = json.dumps(main)
Examination.category = request.POST.get('category')
try:
Examination.notation = request.POST.get('notation')
except AttributeError:
Examination.notation = ''
try:
Examination.name = request.POST.get('name')
except AttributeError:
Examination.name = ''
Examination.save('self')
exam = Examinationform()
main.clear()
return HttpResponse("File uploaded successfuly")
else:
exam = Examinationform()
return render(request,'uploadfileexam1.html',{'form':exam})
模型.py
from django.db import models
from django import forms
class Examination(models.Model):
namefileexam = models.TextField(null=True)
notation = models.CharField(max_length=78, blank=True, null=True)
category = models.CharField(max_length=25)
name = models.CharField(max_length=25, blank=True, null=True)`
enter code here
表格.py
from django import forms
from exam.models import Examination
BOSS_CHOICES=(
('sheets', 'sheets'),
('contestexams', 'contestexams'),
('schoolexams', 'schoolexams'),
('others', 'others'),
)
class Examinationform(forms.Form):
fileexam = forms.FileField(label = 'เลือกไฟล์', required = True, widget=forms.ClearableFileInput(attrs={'multiple': True}))
notation = forms.CharField(label = 'หมายเหตุ', required = False, max_length=78)
category = forms.ChoiceField(label = 'ประเภทของไฟล์', required = True, choices = BOSS_CHOICES)
name = forms.CharField(label = ชื่อผู้อัปโหลด(ใส่ไว้ในเครดิต)', required = False, max_length=78)
当我提交时,它显示了 AttributeError。
“str”对象没有属性“_meta”我认为这个问题是表单views.py。但我不知道我该怎么办。请帮帮我
谢谢你的回答,对不起我的英语能力
解决方案
推荐阅读
- neural-network - 使用 Gumbel Softmax 对中间层进行阈值处理
- php - 如何使用 CURRENT_TIMESTAMP 添加截止日期?
- angular - 如何在对象数组中分配对象?
- c++ - 初始化类的静态数据成员
- apache - 日志文件未显示对 jpeg 的访问权限,但访问发生了
- java - 在模拟 Micronaut 单元测试的类时“递归更新”
- php - PHP在没有循环的情况下获取数组中最长的字符串
- android - 如何使用 Mqtt 编写自定义变量更改侦听器
- java - 想要从应用程序抽屉中隐藏应用程序并想要从另一个应用程序打开
- algorithm - 多个3d形状的外形算法?