gpt4 book ai didi

python - AttributeError:类型对象 'Project' 没有属性 '_meta'

转载 作者:太空宇宙 更新时间:2023-11-04 02:04:33 25 4
gpt4 key购买 nike

当我尝试使用基于类的 View CreateView 时,我在 Django 文件中遇到了以下问题。

AttributeError at /overview/new/
type object 'Project' has no attribute '_meta'

我不知道为什么没有 _meta 属性。我像往常一样从 models.Model 继承了我的模型,但我找不到任何有相同问题的问题。我在下面发布了我的文件,有人可以帮我解决这个问题吗?

views.py:

from django.shortcuts import render, redirect
from django.contrib.auth import logout, authenticate, login
from django.contrib import messages
from django.views.generic import ListView, DetailView, CreateView
from projects.models import Project, Task

class CreateProject(CreateView):
model = Project
fields = ['title', 'description']

def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)

模型.py:

from django.db import models
from django.contrib.auth.models import User
from .CustomIDConstructor import ID


class Project(models.Model):
title = models.CharField(max_length=100, default='No title')
description = models.TextField(default='No description')
author = models.ForeignKey(User, on_delete=models.CASCADE)
customid = models.CharField(max_length=30, default=ID, editable=False, primary_key=True)

def __str__(self):
return self.title

urls.py:

from django.urls import path
from . import views
from projects.views import ProjectsOverview, Project, CreateProject
from django.contrib.auth.decorators import login_required

urlpatterns = [
path('', login_required(views.ProjectsOverview.as_view()), name='projects-projectsoverview'),
path('project/<pk>/', login_required(views.Project.as_view()), name='projects-project'),
path('new/', login_required(views.CreateProject.as_view()), name='projects-newproject')
]

Project_form.html:

{% extends "home/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container mt-4">
<form method="POST">
{% csrf_token %}
{{ form|crispy }}

<button style="background-color:rgb(130, 130, 130); color:white; border: none" class="btn btn-outline-info" type="submit">Create new project</button>
</form>
</div>
{% endblock content %}

完整回溯:

Internal Server Error: /overview/new/
Traceback (most recent call last):
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py", line 168, in get
return super().get(request, *args, **kwargs)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py", line 133, in get
return self.render_to_response(self.get_context_data())
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_data
kwargs['form'] = self.get_form()
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py", line 32, in get_form
form_class = self.get_form_class()
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py", line 101, in get_form_class
return model_forms.modelform_factory(model, fields=self.fields)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\forms\models.py", line 551, in modelform_factory
return type(form)(class_name, (form,), form_class_attrs)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\forms\models.py", line 256, in __new__
apply_limit_choices_to=False,
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\forms\models.py", line 139, in fields_for_model
opts = model._meta
AttributeError: type object 'Project' has no attribute '_meta'
[05/Mar/2019 14:00:15] "GET /overview/new/ HTTP/1.1" 500 115370

最佳答案

从您的 URL 看来,您有一个 View Project 与模型 Project 发生冲突。

path('project/<pk>/', login_required(views.Project.as_view()), name='projects-project'),

最简单的修复方法是重命名该 View ,例如重命名为 ProjectDetail,并更新 URL 模式。

关于python - AttributeError:类型对象 'Project' 没有属性 '_meta',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55002975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com