I am new to Django and python overall. The django experience thus far,
has been great -- immensely satisfying. The only regret I have is not
getting on the django bandwagon earlier.
Anyways, as I've been trying to code away, I hit a stumbling block
while using the newforms and imagefield. the django trunk has changed
quite a bit. and some of the examples I found on the web were using
the older versions of Django.
Here's my problem - I have a userprofile, which has an additional
avatar (ImageField) and a website (URLField). I created a
UserProfileManager as below:
{{ BEGIN CODE }}
<imports go here>
class UserProfileManager(models.Manager):
def create_user(self, username, password, email, avatar, website):
new_user = User.objects.create_user(username, email, password)
new_user.save()
registration_profile = self.create_profile(new_user, avatar,
website)
def create_profile (self, new_user, avatar, website):
return self.create(user=new_user, avatar=avatar,
website=website)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
avatar = models.ImageField(upload_to="/pics/")
website = models.CharField(maxlength=255)
objects = UserProfileManager()
class Meta:
verbose_name = 'User Profile'
class Admin:
pass
def __unicode__(self):
return "User Profile for %s" % self.user
{{ THE FORM IS BELOW }}
class UserProfileForm(forms.Form):
username = forms.CharField(maxlenght=30, label='Username')
password1 =
forms.CharField(widget=forms.PasswordInput(render_value=False),label='Password')
password2 =
forms.CharField(widget=forms.PasswordInput(render_value=False),label='Password
Again')
email = forms.EmailField(label='Email')
avatar = forms.ImageField(upload_to='/pics/')
website = forms.URLField()
def clean_username(self):
if not alnum_re.search(self.cleaned_data['username']):
raise forms.ValidationError('Username can contain only
alphanumeric')
try:
user =
User.objects.get(username__exact=self.cleaned_data['username'])
except User.DoesNotExist:
raise forms.ValidationError('Username already taken')
def clean_avatar(self):
{{END FORM }}
{{BEGIN VIEW}}
def create(request):
if request.method == 'POST':
data = request.POST.copy()
form = UserProfileForm(data, request.FILES)
if form.is_valid():
{{ END VIEW }}
As you would have noticed above, the form, model and view are
incomplete. The reason is because I am not entirely sure on the 'best
practice' for handling the image data..
Based on my understanding, I have the following questions:
1) For the latest SVN Django version, the validation of imagefield
will be handled by django itself (Valid jpg / gif/bmp file)..is that
correct ?
2) The idea is that in the save method of the form, I will call the
UserProfileManager.objects.create_user() method.
Do, I need to pass the actual image as an argument to this method or
will django have saved this filed already ?
IF I do need to pass the actual image (binary data) to the
create_user() method in the model manager, how does the model handle
saving the actual binary data and the filename
Where would i be using the save_FOO_File() and the get_FOO_size()
methods ?
3) Where should I do checking for the size of the file being
uploaded...I know it should go into the form..but any helper methods I
could use ?
Some of these questions may sound really dumb...but bear with me --
I'm still very new to django.
thanks,
-pranav.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---