I'm still a bit confused, apologies. This is the beginning of the view:
def upload_file(request, modelname, id): if modelname == 'film': form = UploadFileForm() else: form = UploadFileForm(show_title=False) object = get_object_or_404(modelname, id__iexact=id) if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) How would I pass in show_title correctly? I tried rewriting the last line: form = UploadFileForm(request.POST, request.FILES, show_title=False) , but I got an error on submission: __init() got multiple values for keyword argument 'show_title' And in terms of correctly passing the variable, does it need to be explicitly passed in the 3rd line above: form = UploadFileForm(), when I want the title shown? Or simply when grabbing the request.POST/ FILES? Thank you for your help so far, Ed On Nov 9, 3:33 am, Knut Ivar Nesheim <knu...@gmail.com> wrote: > The problem is here: > > if request.method == 'POST': > form = UploadFileForm(request.POST, request.FILES) > > You need to make sure you always pass in show_title correctly when > instantiating the form. > > Regards > Knut > > > > > > > > On Tue, Nov 9, 2010 at 3:57 AM, Ed <edmund.rog...@gmail.com> wrote: > > I can fall back on the subclassing suggestion. But I'd like to give > > this one more shot for a fix. I think it has something to do with the > > request.FILES that I need. Here is my complete form: > > > class UploadFileForm(forms.Form): > > def __init__(self, show_title=True, *args, **kwargs): > > super(UploadFileForm, self).__init__(*args, **kwargs) > > if not show_title: > > del self.fields['title'] > > > def clean_file(self): > > content = self.cleaned_data['file'] > > content_type = content.content_type.split('/')[0] > > if content_type in settings.CONTENT_TYPES: > > if content._size > settings.MAX_UPLOAD_SIZE: > > raise forms.ValidationError(_('Please keep > > filesize under %s. > > Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), > > filesizeformat(content._size))) > > else: > > raise forms.ValidationError(_('File type is not > > supported')) > > return content > > > title = forms.CharField(max_length=50) > > file = forms.ImageField(label='Select photo to upload') > > > And here is my complete view: > > > def upload_file(request, modelname, id): > > if modelname == 'film': > > form = UploadFileForm() > > else: > > form = UploadFileForm(show_title=False) > > > object = get_object_or_404(modelname, id__iexact=id) > > if request.method == 'POST': > > form = UploadFileForm(request.POST, request.FILES) > > if form.is_valid(): > > file = request.FILES["file"] > > filename = file.name > > content = file.read() > > > # Assign unique name to file > > new_image_name, extension = unique_name(filename, > > object, > > modelname) > > > #FUTURE: Resize middle and resize remaining in > > background > > #SMALL > > #img_resizer(content) > > u_small = new_image_name + '_small.jpg' > > store_in_s3(u_small, img_resizer(content,250,250,90)) > > > # Save thumbnail url to object > > object.url_small = u_small > > object.save() > > > # Grab Next param to determine where to redirect > > back to > > redirect_to = request.GET.get('next', > > reverse('index_view')) > > return HttpResponseRedirect(redirect_to) > > else: > > # If form validation fails, use original reverse url > > redirect_to = request.GET.get('next', > > reverse('index_view')) > > > else: > > # If first loading form, grab referer and pass to form > > referer = request.META.get('HTTP_REFERER', None) > > > # Pass original location in next url param > > if referer is None: > > redirect_to = reverse('index_view') > > else: > > try: > > redirect_to = urlsplit(referer, 'http', > > False)[2] > > except IndexError: > > redirect_to = reverse('index_view') > > > return render_to_response('upload.html', {'form': form,'obj': > > object,'redirect_to':redirect_to}, context_instance = > > RequestContext(request)) > > > If I remove the def __init__ from the form class, it works perfectly, > > but always shows the title. But with that in the form class, it > > always says "This field is required." For just the imagefield if the > > title is suppressed or for both the title and the imagefield if the > > title is not suppressed. > > > Suggestions? > > > On Nov 8, 3:47 pm, Knut Ivar Nesheim <knu...@gmail.com> wrote: > >> Maybe you could just use subclassing instead of doing stuff at run-time: > > >> class UploadForm(forms.Form): > >> file = ... > >> # custom upload and validation code here > > >> class ThumbnailUploadForm(UploadForm): > >> pass > > >> class UploadFileForm(UploadForm): > >> title = ... > > >> As for your current approach, it looks correct. I've done the same > >> several times and it works as expected. Make sure you are really > >> really passing show_title correctly in your thumbnail case. > > >> Regards > >> Knut > > >> On Mon, Nov 8, 2010 at 9:29 PM, Ed <edmund.rog...@gmail.com> wrote: > >> > I have an image upload form that takes a title and a file for its > >> > field. I have two uses for it. Most of the time I call it, I need both > >> > a title and the image itself. But when I call it simply to grab a > >> > thumbnail, I don't need the title. In fact, the form data is saved to > >> > a different model that doesn't even have title as a field. > > >> > I wanted to suppress the "title" field when I call the form. I could > >> > have created two form classes in my forms.py, but this seemed > >> > unnecessarily repetitious. > > >> > I included the following in my image form class: > > >> > def __init__ (self, show_title=True): > >> > super (BaseClass, self).__init__() > >> > if not show_title: > >> > del self.fields['title'] > > >> > This works great except for one thing. Now I'm getting validation > >> > errors: "This field is required" whether I suppress the title field or > >> > not. Any time I try to submit the form, it tells me I need to enter > >> > data. Any advice on how to do this correctly? > > >> > class UploadFileForm(forms.Form): > >> > def __init__ (self, show_title=True): > >> > super (BaseClass, self).__init__() > >> > if not show_title: > >> > del self.fields['title'] > > >> > title = forms.CharField(max_length=50) > >> > file = forms.ImageField(label='Select photo to upload') > > >> > -- > >> > You received this message because you are subscribed to the Google > >> > Groups "Django users" group. > >> > To post to this group, send email to django-us...@googlegroups.com. > >> > To unsubscribe from this group, send email to > >> > django-users+unsubscr...@googlegroups.com. > >> > For more options, visit this group > >> > athttp://groups.google.com/group/django-users?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.