Difficulty in using windows azure blob services for my project !!

138 views
Skip to first unread message

livelikehimanshu12

unread,
Apr 17, 2015, 10:48:41 AM4/17/15
to django...@googlegroups.com
Hello friends !!
So my project requires me to store images on blob service of windows azure .I am using django in visual studio express . I even have an azure account..
So I wrote the code using a lot help from internet (I was total beginner to django when I started this project ) . My code works perfectly fine for django admin interface i.e. my images are getting stored on azure blobs online and using the stored URL(in my models field) I am able to access it also.

But the problem comes when I am creating my own form which will take image As input from user and upload it to my account's blob service !
The image is not getting stored !! I don't know what's wrong with my code !!
It was working well when I was working with django default filesystem .The same models.py works for admim interface but for my normal GUI it is not working !!

One more thing , I created my form as we do in normal HTML and I am not using django form class !!
Plzz help me !! I am totally stuck .
I am a newbie in django !!
######################################################### models.py ###################################################################################
.....
....
def get_upload_file_name(instance, filename):
    return "assets/uploaded_files/%s_%s" % (str(time()).replace('.','_'), filename)

class Account(models.Model):
    email =models.EmailField(max_length=254,primary_key= True)
    password =models.CharField(max_length=20)
    name =models.CharField(max_length=50)
    dob_d=models.IntegerField(max_length=2)
    dob_m=models.IntegerField(max_length=2)
    dob_y=models.IntegerField(max_length=4)

class Accountinfo(models.Model):
    email = models.ForeignKey(Account,primary_key=True)
    pic=models.FileField(max_length=255, storage=AzureStorage(account_name="clique",account_key="xxx",container="new-public-container"),upload_to=get_upload_file_name) 
    about=models.CharField(max_length=254)

    def get_thumbnail(self):
        thumb = str(self.pic)
        return thumb

#################################################################### views.py ################################################################
.....
.....
....
def uploadpic(request):
    if request.method=="POST":
        imail=request.POST.get('inputemail')
        dbaccount=Account.objects.get(email=imail)
        ifile=request.POST.get('inputfile')
        print(type(ifile))
        iabout=request.POST.get('inputabout')
        account1=Accountinfo(email=dbaccount,pic=ifile,about=iabout)
        account1.save()
        print(ifile)
        return render(request,'success.html',{"message":"congrats","name":imail})

################################################################### my template profile.html ################################################################
....
...
...
<form class="form-signin" action="{% url 'uploadpic' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h2 class="form-signin-heading">upload</h2>
        
                                    <label for="inputEmail" class="sr-only">Email address</label>
                                    <input type="email" id="inputemail" name="inputemail" class="form-control" placeholder="Email address" required autofocus>
                                    
                                    <label for="inputabout" class="sr-only">ABOUT YOURSELF</label>
                                    <input type="text" id="inputabout" name="inputabout" class="form-control" placeholder="About" required autofocus />

<label for="inputfile" class="sr-only">Select</label>
<input type="file" id="inputfile" name="inputfile" class="form-control" placeholder="file" required>
        
<button class="btn btn-lg btn-primary btn-block" type="submit">upload</button>
                                </form>

This thing is working via admin interface , but through my created form it is not!!!
Mt file is not getting stored !!!

Tom Evans

unread,
Apr 17, 2015, 11:29:40 AM4/17/15
to django...@googlegroups.com
This isn't how to handle uploaded files - file data is stored in
request.FILES. There is a section in the documentation explaining the
simple file upload case, see here:

https://docs.djangoproject.com/en/1.8/topics/http/file-uploads/#basic-file-uploads

You would be well advised to use a ModelForm to process data that you
post to a view in order to create items:

https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#module-django.forms.models

and use an ImageField to process the file:

https://docs.djangoproject.com/en/1.8/ref/forms/fields/#imagefield

Cheers

Tom

HIMANSHU RANJAN

unread,
Apr 17, 2015, 2:21:48 PM4/17/15
to django...@googlegroups.com
Ohh thanks Tom 
it helped !!
In views.py i changed ifile=request.POST.get('inputfile') to 
                                        ifile=request.FILES['inputfile'] and it worked....
Thanks a lot !!!


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1KC6NvGnY_utK-ujF2nozkmWh_YQ_XW3-zHQRWwdnozMA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages