django' Commande'对象没有属性' __ name_

时间:2014-11-11 18:38:31

标签: python django

嗨stackoverflow人,

在我的项目中,我尝试编写一个视图,它将管理外部api,它将用于获取某些数据,呈现它们并将它们存储在数据库中。

当我尝试访问我的视图时,遇到以下错误

回溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/commande/recherche

Django Version: 1.7.1
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'commands')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  87.                 response = middleware_method(request)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/middleware/common.py" in process_request
  72.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in is_valid_path
  619.         resolve(path, urlconf)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
  494.     return get_resolver(urlconf).resolve(path)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
  345.                     sub_match = pattern.resolve(new_path)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
  345.                     sub_match = pattern.resolve(new_path)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
  224.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in callback
  231.         self._callback = get_callable(self._callback_str)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/functools.py" in wrapper
  434.                 result = user_function(*args, **kwds)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in get_callable
  97.             mod = import_module(mod_name)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/importlib/__init__.py" in import_module
  109.     return _bootstrap._gcd_import(name[level:], package, level)
File "/home/user/workspace/Python/commands-project/project/commands/views.py" in <module>
  6. from .form import CommandesForm, CommandeForm
File "/home/user/workspace/Python/commands-project/project/commands/form.py" in <module>
  11. class CommandeForm(forms.ModelForm):
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/forms/models.py" in __new__
  293.                                      opts.model.__name__)

Exception Type: AttributeError at /commande/recherche
Exception Value: 'Commande' object has no attribute '__name__'

我的 models.py:

from django.db import models


class Client(models.Model):
    client = models.IntegerField(null=True)


class Commandes(models.Model):
    date_debut = models.DateField()
    date_fin = models.DateField()
    id_groups = models.CharField(max_length=100)
    id_client = models.ForeignKey(Client)


class Commande(models.Model):
    id_flux = models.CharField(max_length=100, null=True, blank=True)
    id_commande = models.CharField(max_length=100, null=True, blank=True)
    id_client = models.ForeignKey(Client)

我的 views.py

from django.shortcuts import render
from http.client import HTTPConnection
from urllib.parse import urlparse, urlunparse
from .form import CommandesForm, CommandeForm
import requests

def resultat(request):
    return render(request, 'commands/resultat.html')


def recherche(request):
    if request.method == 'POST':
        if 'Commandes' in request.POST:
            pass

        if 'Commande' in request.POST:
            pass
    else:
        formCommandes = CommandesForm()
       formCommande = CommandeForm()

    return render(request, 'commands/recherche.html', {'formCommandes': formCommandes })

我的 form.py

from django import forms
from .models import Commande, Commandes


class CommandesForm(forms.ModelForm):
    class Meta:
        model = Commandes()
        fields = ('date_debut', 'date_fin', 'id_groups')


class CommandeForm(forms.ModelForm):
    class Meta:
        model = Commande()
        fields = ('date_debut', 'date_fin', 'id_groups', 'id_client', 'id_flux',
                  'id_commande')

这是我需要管理的以下异常: &#39; COMMANDE&#39;对象没有属性&#39; 名称&#39;

我知道&#39; 名称&#39;属性在类中而不是实例中。 我无法弄清楚的事实是,为什么例外情况会随着Commande&#39;而提出。对象而不是&#39;命令&#39;对象

0 个答案:

没有答案