Friday, June 24, 2011

upload file isn't working

# models.py #

def content_file_name(instance, filename):
return '/'.join(['uploads', instance.user.username, 'docs',
filename])

class upload_model(User):
user = models.ForeignKey(User, related_name = 'user_upload')
title = models.CharField(max_length = 50)
doc = models.FileField(upload_to = content_file_name)
objects = UserManager()

# admin.py #

class upload_admin(admin.ModelAdmin):
fields = ['user', 'title', 'doc']

admin.site.register(upload_model, upload_admin)

# Uforms.py #

class upload_form(ModelForm):
class Meta:
model = upload_model
fields=['title', 'doc']

title = forms.CharField(max_length = 50, label = _('Document
Title:'), required = True, help_text = _("This field is required."))
doc = forms.FileField()


# views2.py #

def handle_uploads(request, key): #title):
saved=[]

upload_dir = settings.UPLOAD_PATH % request.user.username
upload_full_path =os.path.join(settings.MEDIA_ROOT, upload_dir)

if not os.path.exists(upload_full_path):
os.makedirs(upload_full_path)

for key in keys:
if key in request.FILES:
upload = request.FILES[key]
while os.path.exists(os.path.join(upload_full_path,
upload.name)):
if (request.user.username not in upload.name) and
(request.user.first_name not in upload.name):
upload.name = request.user.username + "_" +
upload.name
dest = open(os.path.join(upload_full_path, upload.name),
'wb')
for chunk in upload.chunks():
dest.write(chunk)
dest.close()
saved.append((key, os.path.join(upload_dir,
upload.name)) )
return saved

def upload_view(request):
user = request.user
if user.is_authenticated():
if request.method == 'POST':
form =upload_form(request.POST, request.FILES, user)
if form.is_valid():
file_instance = upload_model()
saved_file = handle_uploads(request, ['doc'])
#request.POST.get('title'))
for f in saved_file:
setattr(file_instance, f[0])
file_instance.save()
return HttpResponseRedirect('/user/update/success/')
else:
form = upload_form()
return render_to_response('/home/oneadmin/webapps/oneadmin/
oneadmin/templates/oneadmissions/documents.html', {'form':form},
context_instance = RequestContext(request))

my urls.py file has a url that links to the upload_view. I just cant
seem to find why the file isn't uploading. Whenever I try to upload
something, it just refreshes the page and states that the given fields
are required (even though I entered all the information). Help please.

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment