属性错误:属性错误 在/menu/register/ 中的 AttributeError at /menu/register/ --'function' 对象没有属性‘objects’

2024-04-24 22:51:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这是完整的代码。当我试图构建登录和注册表单时,会发生此错误!在

错误`

AttributeError at /menu/register/
'function' object has no attribute 'objects'
Request Method: POST
Request URL:    http://127.0.0.1:8000/menu/register/
Django Version: 1.11
Exception Type: AttributeError
Exception Value:    
'function' object has no attribute 'objects'
Exception Location: /Users/ambuj/Documents/GitHub/TrailPOS/pos/menu/forms.py in clean_username, line 70
Python Executable:  /usr/bin/python
Python Version: 2.7.10
Python Path:    
['/Users/ambuj/Documents/GitHub/TrailPOS/pos',
 '/Library/Python/2.7/site-packages/Django-1.11-py2.7.egg',
 '/Library/Python/2.7/site-packages/twilio-6.3.dev0-py2.7.egg',
 '/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Library/Python/2.7/site-packages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
Server time:    Wed, 14 Jun 2017 10:44:22 +0000`

我的视图.py

^{pr2}$

我的模型.py

from django.db import models
from django.conf import settings


class category(models.Model):
    categoryName = models.CharField(max_length=50)

    def __str__(self):
        return self.categoryName


class dish(models.Model):
    BOOL_CHOICES = ((True,'Veg'),(False,'Non-Veg'))
    dishid = models.AutoField(primary_key=True)
    dishName = models.CharField(max_length=200)
    dishCuisine = models.CharField(max_length=100)
    dishContent = models.TextField(max_length=1000)
    dishType = models.BooleanField(choices=BOOL_CHOICES)
    dishCategory = models.ForeignKey(category, on_delete=models.CASCADE)
    dishHalfPrice = models.DecimalField(max_digits=7,decimal_places=3,default=0)
    dishFullPrice = models.DecimalField(max_digits=7,decimal_places=3,default=0)
    dishCostPrice = models.DecimalField(max_digits=7,decimal_places=3,default=0)

    def __str__(self):
        return self.dishID

    def __str__(self):
        return self.dishName

    def save(self, *args, **kwargs):
        if not self.pk:
            self.full_clean()
        super(dish, self).save(*args, **kwargs)

    #def clean(self, *args, **kwargs):
    #    dishName = self.dishName
    #    if dishName == "":
    #        raise ValidationError("Cannot be empty")
    #    return super(dish, self).clean(*args, **kwargs)


class discount(models.Model):
    APPLICATION_CHOICES = ((True, 'Single Item'), (False, 'Final Bill'))
    discountName = models.CharField(max_length=200)
    discountDish = models.ForeignKey(dish, on_delete=models.CASCADE)
    discountRate = models.IntegerField()
    discountDescription = models.TextField(max_length=1000)
    discountApp = models.BooleanField(choices=APPLICATION_CHOICES)

    def __str__(self):
        return self.discountName


class tax(models.Model):
    APPLICATION_CHOICES = ((True, 'Single Item'), (False, 'Final Bill'))
    taxName = models.CharField(max_length=200)
    taxDish = models.ForeignKey(dish, on_delete=models.CASCADE)
    taxRate = models.IntegerField()
    taxDescription = models.TextField(max_length=1000)
    taxApp = models.BooleanField(choices=APPLICATION_CHOICES)

    def __str__(self):
        return self.taxName


class Order(models.Model):
    order_user = models.CharField(max_length=50)
    order_list = models.CharField(max_length=1000)
    order_totalprice = models.DecimalField(max_digits=7,decimal_places=3,default=0)


class Cash(models.Model):
    cash_amount = models.DecimalField(max_digits=7, decimal_places=3,default=0)

我的表单.py

from django import forms
from .models import dish
from .models import category
from .models import discount
from .models import tax
from django.contrib.auth import get_user_model

User = get_user_model

class dishModelForm(forms.ModelForm):
    class Meta:
        model = dish
        fields = [
            "dishName",
            "dishCuisine",
            "dishContent",
            "dishType",
            "dishCategory",
            "dishHalfPrice",
            "dishFullPrice",
            "dishCostPrice"
        ]


class categoryModelForm(forms.ModelForm):
    class Meta:
        model = category
        fields = [
            "categoryName"
        ]

class discountModelForm(forms.ModelForm):
    class Meta:
        model = discount
        fields = [
            "discountName",
            "discountDish",
            "discountRate",
            "discountDescription",
            "discountApp"
        ]

class taxModelForm(forms.ModelForm):
    class Meta:
        model = tax
        fields = [
            "taxName",
            "taxDish",
            "taxRate",
            "taxDescription",
            "taxApp"
        ]

class UserRegisterModelForm(forms.Form):
    username = forms.CharField()
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)


    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("Password must match")
        return password2

    def clean_username(self):
        username = self.cleaned_data.get('username')
        if User.objects.filter(username__icontains=username).exists():
            raise forms.ValidationError("This username is taken")
        return username

    def clean_email(self):
        email = self.cleaned_data.get('email')
        if User.objects.filter(email__icontains=email).exists():
            raise forms.ValidationError("This email is already registered.")
        return email

我的html文件

{% extends "menu/base.html" %}

{% block content %}
<div class='row'>
<div class='col-sm-6 col-sm-offset-3'>
  <h1>Register</h1>
  <form class='form' method='POST' action="">
    {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="Register" />
  </form>
</div>
</div>
{% endblock %}

Tags: selfmodelslibdeflibraryusernameformsframework
1条回答
网友
1楼 · 发布于 2024-04-24 22:51:40

当您在表单.py. 在

User = get_user_model()

而且,请不要多次剪切和粘贴文本;这是有原因的,因此要求您编写的文本数量要比代码少;在您的例子中,您已经发布了大量与您的问题无关的代码。在

相关问题 更多 >