首页 > 解决方案 > 如何处理 '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。但我不知道我该怎么办。请帮帮我

谢谢你的回答,对不起我的英语能力

标签: pythondjangoformsmodelattributeerror

解决方案


推荐阅读