简体   繁体   中英

'tuple' object has no attribute 'startswith'

I am working on an old open source app, and I keep on getting the following error every time I try to add a new invoice instance:

AttributeError at /admin/invoices/invoiceheaders/add/ 'tuple' object has no attribute 'startswith'

I am working with two (2) modules: views.py and models.py:

views.py

@login_required
def new(request):
    error = ''
    if request.method == 'POST':
        itemFound = False
        for id in range(1, int(request.POST['numberitem']) + 1):
            if request.POST.get('Item_%s' %id):
                itemFound = True
                break
        if itemFound:
            user = User.objects.get(id = request.user.id)
            issuer =  Contacts.objects.get(id = int(request.POST['Issuer']) )
        customer = Contacts.objects.get(id = int(request.POST['Custumer']) )
        payterm = Payterms.objects.get(id = int(request.POST['Payterm']) )
        ihd = InvoiceHeaders()
        ihd.user = user
        ihd.issuer = issuer
        ihd.payterm = payterm
        ihd.customer = customer
        ihd.status = 'D'
        ihd.findrandomnumber=sha224( "%f"%(time()) ).hexdigest()

        ihd.save()

        for id in range(1, int(request.POST['numberitem']) + 1):
            if request.POST.get('Item_%s' %id):
                ibd = InvoiceBodies()
                ibd.header = ihd
                ibd.quantity = float(request.POST['Item_qta_%s' %id].replace(',','.'))
                items = Items.objects.get(id = int(request.POST['Item_%s' %id]) )
                ibd.item = items
                ibd.extraDescription = request.POST['Item_descr_%s' %id].strip()
                ibd.save()
        return HttpResponseRedirect(reverse_lazy('invoices:index'))
    else:
        error = _('Item is obbligatory')

form = formInvoice(uid=request.user.id) # An unbound form
custumer_selected = 3
return render(request, 'invoices/new2.html' ,
                        {'form': form,
                         'custumer_selected' : custumer_selected ,
                         'error' : error, })

models.py

def content_file_name(instance, filename):
    return '/'.join([MEDIA_URL_USER_DOCS, '1' , filename])

class InvoiceHeaders(models.Model):
    issuer = models.ForeignKey(Contacts, related_name='invoices_issuer', limit_choices_to={"tp_contacts":"P", })
    customer = models.ForeignKey(Contacts, related_name='invoices_custumer', limit_choices_to={"tp_contacts":"C", })
    payterm = models.ForeignKey(Payterms, related_name='invoices_payterm')
    user = models.ForeignKey(User)
    pdf = models.FileField(upload_to=content_file_name)
    findrandomnumber = models.CharField(unique=True,max_length=56)

    number = models.PositiveIntegerField(null=True, blank=True, verbose_name = _("Number issue"))
    yearissue = models.PositiveIntegerField(null=True, blank=True, verbose_name = _("Year issue"))
    dateissue = models.DateField(null=True, blank=True, verbose_name = _("Date issue"))
    status = models.CharField(max_length=1, choices=INVOICE_STATUS, verbose_name=_("status"))

    issuer_name = models.CharField(max_length = 80, verbose_name = _("My Name"))
    issuer_surname = models.CharField(max_length = 80, verbose_name = _("My Surname"))
    issuer_address = models.CharField(max_length = 80, verbose_name = _("My Address"))
    issuer_zipcode = models.CharField(max_length = 10, verbose_name = _("My Zip Code"))
    issuer_town = models.CharField(max_length = 50, verbose_name = _("My Town"))
    issuer_province = models.CharField(max_length = 2, verbose_name = _("My Province"))
    issuer_vatnumber = models.CharField(max_length = 11, verbose_name  =     _("My VAT number"))

    customer_name = models.CharField(max_length = 80, verbose_name = _("Customer Name"))
    customer_surname = models.CharField(max_length = 80, verbose_name = _("Customer Surname"))
    customer_address = models.CharField(max_length = 80, verbose_name = _("Customer Address"))
    customer_zipcode = models.CharField(max_length = 10, verbose_name = _("Customer Zip Code"))
    customer_town = models.CharField(max_length = 50, verbose_name = _("Customer Town"))
    customer_province = models.CharField(max_length = 2, verbose_name = _("Customer Province"))
    customer_vatnumber = models.CharField(null=True,  blank=True,  max_length = 11, verbose_name = _("Customer VAT number"))

    pay_term01 = models.CharField(null=True, blank=True, max_length =  80, verbose_name = _("Payterm 1"))
    pay_term02 = models.CharField(null=True, blank=True, max_length = 80, verbose_name = _("Payterm 2"))
    pay_term03 = models.CharField(null=True, blank=True, max_length = 80, verbose_name = _("Payterm 3"))


    def __unicode__(self):
        return u"%s - %s, %s" %(self.number, self.dateissue, self.customer_name)

    class Meta:
        verbose_name_plural = _('Invoice Headers')
        verbose_name  = _('Invoice Header')
        unique_together = (('number', 'yearissue', 'user'),)



    def nextnumber(self, uid = None, year = None):
        return self.maxnumber(uid = uid, year = year) + 1

    def maxnumber(self, uid = None, year = None):
        if uid == None:
            return -1
        if year == None:
               year = datetime.now().year
        maxnum = InvoiceHeaders.objects.filter(user = 
uid).filter(dateissue__year = year).aggregate(Max('number'))
        num = maxnum['number__max']
        if num:
            return num
        else:
            return 0

    def save(self, number=None, dateissue=None):
        #force = False
        if self.status == 'I' and self.dateissue == None:
            if dateissue == None:
                self.dateissue = datetime.today()
                self.yearissue = datetime.today().year
            else:
                self.dateissue = dateissue
                self.yearissue = dateissue[0:4]
            if number == None:
                self.number = self.nextnumber(uid = self.user, year = self.dateissue.year )
            else:
                self.number = number


        self.issuer_name = self.issuer.name
        self.issuer_surname = self.issuer.surname
        self.issuer_address = self.issuer.address
        self.issuer_zipcode = self.issuer.zipcode
        self.issuer_town = self.issuer.town
        self.issuer_province = self.issuer.province
        self.issuer_vatnumber = self.issuer.vatnumber

    self.customer_name = self.customer.name
    self.customer_surname = self.customer.surname
    self.customer_address = self.customer.address
    self.customer_zipcode = self.customer.zipcode
    self.customer_town = self.customer.town
    self.customer_province = self.customer.province
    self.customer_vatnumber = self.customer.vatnumber

    self.pay_term01 = self.payterm.description1
    self.pay_term02 = self.payterm.description2
    self.pay_term03 = self.payterm.description3

    super(InvoiceHeaders, self).save()

sittings.py

import os  

from os.path import abspath, basename, dirname, join, normpath

# Build paths inside the project like this: os.path.join(BASE_DIR, 
...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

import socket

import sys

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

if socket.gethostname() == 'localhost':
    DEBUG = False
else:
    DEBUG = True


ALLOWED_HOSTS = []

ROOT_URLCONF = 'urls'

DATETIME_FORMAT= "j N Y P"
DATE_FORMAT= "j N Y P"

#email info
EMAIL_USE_TLS = True
EMAIL_HOST = '***********'
EMAIL_HOST_USER = 'lablinux At gmail dOt com'
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = 587

ADMINS = (
('Michele', 'lablinux@gmail.com'),
)

MANAGERS = ADMINS



DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}



TIME_ZONE = 'Europe/Rome'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'it-IT'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as 
not
# to load the internationalization machinery.
USE_I18N = True
LANGUAGE_CODE = 'it'
LANGUAGES = (
            ('it',  'Italiano'),
            ('en',  'English'),
)

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
#MEDIA_ROOT = ''
#MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_ROOT = '%s/media' %PROJECT_PATH,
MEDIA_URL_USER_DOCS = '%s/userdocs' %MEDIA_ROOT



MEDIA_URL = '/media/'

LOGIN_REDIRECT_URL = '/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure 
to 
use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/djangomedia/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'a4vy20$loa80-k3z7s!n#zt8*dgqvc)4j-yy$sox)aqm3m@gbk'



MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',

)


INTERNAL_IPS = (
    '127.0.0.1',
)




TEMPLATES = [  
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR,'templates')],
    #'APP_DIRS': True,
    'OPTIONS': {
        'debug': DEBUG,

        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.i18n',
        ],
        'loaders': [
             'django.template.loaders.filesystem.Loader',
             'django.template.loaders.app_directories.Loader',
              ]
        },
    },
]

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.staticfiles',
    'bootstrap3',
    'contacts',
    'payterms',
    'items',
    'invoices',
    'fees',
    'home',
    'profiles',
 ]

DATE_FORMAT = "j F Y"


AUTH_PROFILE_MODULE = 'profiles.ProfileUser'

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")

    ]

STATIC_ROOT =  os.path.join(os.path.dirname(BASE_DIR), "static_cdn")

MEDIA_ROOT should be string, you are creating a tupple.

You are adding a ', 'comma, remove it to solve the issue. It is creating a tupple, we need a String.

in settings.py

MEDIA_ROOT = '%s/media' %PROJECT_PATH, change this to MEDIA_ROOT = '%s/media' %PROJECT_PATH it will solve the issue.

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