简体   繁体   中英

AttributeError: type object 'Project' has no attribute '_meta'

I encountered the following in my Django files when I attempt to use the class based view CreateView .

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

I have no idea why the _meta attribute is not there. I inherited my model from models.Model as usual, but I can't find any questions that have the same problem. I posted my files down below, can somebody help me fix this problem?

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)

models.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 %}

Full traceback:

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

From your URLs, it looks like you have a view Project which is clashing with the model Project .

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

The simplest fix would be to rename that view, for example to ProjectDetail , and update the URL pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM