Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:30:14+00:00 2026-05-31T01:30:14+00:00

So here is my form: class AvatarUploadForm(forms.Form): avatar = forms.ImageField(label=’Image’, help_text=’Maximum size: 100x100px’) #

  • 0

So here is my form:

class AvatarUploadForm(forms.Form):
    avatar = forms.ImageField(label='Image', help_text='Maximum size: 100x100px')

    # Validation stuff down here.

And model:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    post_count = models.IntegerField(default=0)
    avatar = models.ImageField(null=True, blank=True, upload_to='images/avatars')
    # ...

And the currently unfinished and potentially buggy view:

@login_required
def user(request, username):
    user = get_object_or_404(User, username=username)
    profile = user.profile
    if request.user == user:
        if request.method == 'POST':
            form = AvatarUploadForm(request.POST, request.FILES)
            if form.is_valid():
                 profile.avatar = request.FILES['avatar']
                 profile.save()
                 return render_to_response('forum/user.html', {'profile_user': user, 'profile': profile},
                     context_instance=RequestContext(request))
            else:
                form = AvatarUploadForm()

            return render_to_response('forum/user.html', { 'profile_user': user, 'profile': profile, 'form': form },
                context_instance=RequestContext(request))

    return render_to_response('forum/user.html', { 'profile_user': user, 'profile': profile })

What I want is to keep the original file extension, .png, .gif or whatever, but change the filename to be the user_id or username of the user.

Since when registering, users don’t upload an avatar and profiles are created when you view the page the avatar upload form is one, we can assume that the user profile already exists.

I’m thinking the solution lies somewhere in making upload_to a callable, but I’m not entirely sure.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:30:16+00:00Added an answer on May 31, 2026 at 1:30 am

    You are correct. upload_to can be a callable. Here is an excerpt from a project of mine that you can adapt to your specifications. This does more than what you asked for, but I think it shows what kind of flexibility you can have.

    import os
    import random
    import string
    from datetime import datetime
    
    ....
    
    def generate_random_string(digit_length=6, char_length=6):
        digits = "".join( [random.choice(string.digits) for i in xrange(6)] )
        chars = "".join( [random.choice(string.letters) for i in xrange(6)] )
        return digits + chars
    
    def item_upload_to(instance, filename):
        file_root, file_ext = os.path.splitext(filename)
        date = datetime.now().strftime("%Y/%m/%d")
        random_name = generate_random_string() + file_ext
        return '/'.join(['user-media',date, random_name])
    
    ....
    
    
    class GenericImage(models.Model):
        user = models.ForeignKey(User, default=NEWAVENUE_USER_ID)
        original_image = models.ImageField(upload_to=item_upload_to)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my form: class IPTrackerSearchForm(forms.Form): keyword = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'size':'50'})) search_in = forms.ChoiceField(required=False, choices=ANY_CHOICE
I'm trying to create a form. Here is my form class: class RegisterForm(forms.Form): login=forms.CharField(min_length=5,max_length=15)
My form: class PlanForm(forms.ModelForm): owner = forms.ModelChoiceField(label=, queryset=Profile.objects.all(), widget=forms.HiddenInput()) etc... class Meta: model =
I have a simple form class MyForm(forms.Form): ... fieldname = forms.CharField(help_text=Some help text) I
here is what my code looks like i have two forms: class Form_1 extends
Here's my Zend code: <?php require_once ('Zend\Form.php'); class Sergio_Form_registrationform extends Zend_Form { public function
Here's what I'd like: Here's my code: <?php require_once ('Zend\Form.php'); class Sergio_Form_registrationform extends Zend_Form
Here is the input form code <tr valign=top> <td align=right class=innertablestyle><font class=normal><strong>Homepage</strong></font></td> <td>&nbsp;</td> <td>
Let's say I have this form: class SimpleUploadForm(forms.Form): file = forms.FileField() I have this
So, I have the following form: class DesignItemForm (forms.ModelForm): def __init__(self, *args, **kwargs): super(DesignItemForm,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.