True but I wanted to organize my folder a bit because there's not just one photo per pet, there could be quite a lot more than one photo per pet. First, I'd rather not have a million _ at the end of a file because everyone named their file cute1.jpg. Anyway, I got it to work, code below. (It's not the greatest code I've ever written but it seems to work)
class AddPhotoForm(forms.Form): # Categories is just a list of photo categories that a user can choose from. # I disable them by selecting show_in_sidebar=False, hence the filter categories = Category.objects.filter(show_in_sidebar=True).values_list('id','name') title = forms.CharField(max_length=100) category = forms.ChoiceField(choices=categories) image = forms.ImageField() def handle_uploaded_file(f, pet_id): filename = f.name while os.path.exists(os.path.join(settings.MEDIA_ROOT, 'pet_photos', pet_id, filename)): if filename[len(filename[:-4]):-3] == '.': # 3 char extension (ie. .jpg) filename = filename[:-4] + '_' + filename[len(filename[:-4]):] elif filename[len(filename[:-5]):-4] == '.': # 4 char extension (ie. .jpeg) filename = filename[:-5] + '_' + filename[len(filename[:-5]):] else: # who knows, append it to the beginning filename = '_' + filename destination = open(os.path.join(settings.MEDIA_ROOT, 'pet_photos', pet_id, filename), 'wb+') for chunk in f.chunks(): destination.write(chunk) return os.path.join('pet_photos', pet_id, filename) def add_pet_photo(request, pet_id): this_pet = get_object_or_404(Pet, id=pet_id) # using custom user model because I'm rewriting a web app user = User.objects.get(username__iexact=request.session['username']) if request.method == 'POST': if this_pet.owner == user: # Make sure pet belongs to this user form = AddPhotoForm(data=request.POST, files=request.FILES) if form.is_valid(): filename = handle_uploaded_file(request.FILES['image'], pet_id) c = Category.objects.get(id=form.cleaned_data['category']) m = Media(title=form.cleaned_data['title'], category=c, type='P', pet=this_pet, image=filename) m.save() return HttpResponseRedirect('/photos/' + pet_id) else: return HttpResponse("Not your pet") else: form = AddPhotoForm() return render_to_response('photos/add_photo.html', {'pet': this_pet, 'form': form }, context_instance=RequestContext(request)) So it seems to work, probably not the most pythonic way of doing it but it gets the job done. -Chris On Sep 3, 9:00 am, TiNo <[EMAIL PROTECTED]> wrote: > Question, why do you need to create a folder for every pet? Why not throw > all pet photos in one folder?That would solve your problem as well. > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---