Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"not all arguments converted during string formatting" Python Django

Tags:

python

django

I am programming in Django 1.5 with Python 2.7 on Windows Vista. I am trying to create user profiles. However, when I visit localhost:8000/admin/home/userprofile, I got the 1146, "Table 'demo.home_userprofile' doesn't exist error. Now I have in models.py :

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class userProfile(models.Model):

    def url(self, filename):
        ruta = "MultimediaData/Users/$s/%s"%(self.user.username, filename)
        return ruta

    user = models.OneToOneField(User)
    photo = models.ImageField(upload_to = url)
    telefono = models.CharField(max_length = 30)

    def __unicode__(self):
        return self.user.username

And Django page is pointing not all arguments converted during string formatting error at me. This is a page that allows user to upload picture and phone number. What seems to be the problem?

like image 415
Concerned_Citizen Avatar asked Mar 17 '13 01:03

Concerned_Citizen


People also ask

How do you fix not all arguments converted during string formatting?

Such a common error is TypeError: not all arguments converted during string formatting. This error is caused when there is a mismatch in data types and strings are not properly formatted. The solution to this error is to use proper string formatting functions such as int() or str() to obtain the desired data type.

What does not all arguments converted during string formatting mean?

The “not all arguments converted during string formatting” error is raised when Python does not add in all arguments to a string format operation. This happens if you mix up your string formatting syntax or if you try to perform a modulo operation on a string.

How do you format a string in Python?

To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.


2 Answers

Change:

ruta = "MultimediaData/Users/$s/%s"%(self.user.username, filename)

To:

ruta = "MultimediaData/Users/%s/%s"%(self.user.username, filename)
#                            ^ Notice the sign change

You seem to have used a $ instead of a %, which was the problem.

like image 128
Volatility Avatar answered Oct 12 '22 23:10

Volatility


To make it compaitble with Python 2 or 3...

ruta = "MultimediaData/Users/{0}/{1}".format(self.user.username, filename)
like image 39
Brandon Avatar answered Oct 13 '22 01:10

Brandon