Как я могу решить проблему AttributeError?
from django.shortcuts import render
from django.http import HttpResponse
from . import models
from .models import *
import csv
def index(request):
return HttpResponse("Hello, World. You're at the index.")
def uploadFile(request):
if request.method == "POST":
# Fetching the form data
fileTitle = request.POST["fileTitle"]
uploadedFile = request.FILES["uploadedFile"]
# Saving the information in the database
document = models.Document(
title = fileTitle,
uploadedFile = uploadedFile
)
document.save()
documents = models.Document.objects.all()
return render(request, "upload/upload-file.html", context = {"files": documents})
def csvToModel(request):
def csvToModel(request):
path = 'C:/Users/taink/Desktop/attach/email.csv'
file = open(path)
reader = csv.reader(file)
Я пытаюсь объединить два проекта django, которые хорошо работали при запуске друг друга, но он продолжает получать ошибки всякий раз, когда я ввожу этот код в views.py.
from django.db import models
class Document(models.Model):
title = models.CharField(max_length = 200)
uploadedFile = models.FileField(upload_to = "Uploaded Files/")
dateTimeOfUpload = models.DateTimeField(auto_now = True)
class seops(models.Model):
name = models.CharField(max_length=50)
email = models.CharField(max_length=50)
def __str__(self):
return "이름 : " + self.name + ", 주소 : "+ self.email
вот models.py
Traceback (most recent call last):
File "C:\Users\taink\PycharmProjects\upload\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\taink\PycharmProjects\upload\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\taink\PycharmProjects\upload\upload\views.py", line 23, in uploadFile
documents = models.document.objects.all()
Exception Type: AttributeError at /upload/
Exception Value: module 'django.db.models' has no attribute 'document'
Даже если после ошибки код разделяется и выполняется снова, та же ошибка продолжается. В чем проблема?
Вы должны добавить следующее к вашему view.py
from .models import Document, seops
При хорошей практике первая буква имени класса seops пишется заглавной Seops
для получения всех документов: documents = Document.objects.all()