-2

Can someone give a basic idea on how to approach the problem. It's not working on localhost and blobstore is also not working. Is there any other method or can someone show me the correct approach? I am only showing the code related to the query.... enter image description hereHere is the pic of error

html code

<div class="form-group">
            <input type="file" class="form-control" name="profile_pic" value="" placeholder="profile_pic">
            <div class="error">
            {{error_username}}
          </div>
        </div>

Takes input from form.

class Signup(BlogHandler): def get(self): self.render("signup-form.html")

def post(self):
    have_error = False
    self.username = self.request.get('username')
    self.profile_pic = self.request.get('profile_pic')   
    self.password = self.request.get('password')
    self.verify = self.request.get('verify')
    self.email = self.request.get('email')
    params = dict(username = self.username,
                  email = self.email)

    if not valid_username(self.username):
        params['error_username'] = "That's not a valid username."
        have_error = True

    if not valid_password(self.password):
        params['error_password'] = "That wasn't a valid password."
        have_error = True
    elif self.password != self.verify:
        params['error_verify'] = "Your passwords didn't match."
        have_error = True

    if not valid_email(self.email):
        params['error_email'] = "That's not a valid email."
        have_error = True

    if have_error:
        self.render('signup-form.html', **params)
    else:
        self.done()

def done(self, *a, **kw):
    raise NotImplementedError

class to pass parameters to register function in User class

class Register(Signup,blobstore_handlers.BlobstoreUploadHandler):   
    def done(self):
        u = User.by_name(self.username)
        if u:
            msg = 'That user already exists.'
            self.render('signup-form.html', error_username = msg)
        else:
            upload = self.get_uploads()[0]
            u = User.register(self.username, self.password, upload.key(), self.email)
            u.put()

            self.login(u)
            self.redirect('/blog')

The user Model. Parameter profile_pic is passed and assigned profile_pic to column profile_pic in User entity

class User(db.Model):
    name = db.StringProperty(required = True)
    pw_hash = db.StringProperty(required = True)
    email = db.StringProperty()
    profile_pic = db.BlobProperty()

    @classmethod
    def by_id(cls, uid):
        return User.get_by_id(uid, parent = users_key())

    @classmethod
    def by_name(cls, name):
        u = User.all().filter('name =', name).get()
        return u

    @classmethod
    def register(cls, name, pw, profile_pic, email = None):
        pw_hash = make_pw_hash(name, pw)
        return User(parent = users_key(),
                    name = name,
                    pw_hash = pw_hash,
                    profile_pic = profile_pic,  
                    email = email)

    @classmethod
    def login(cls, name, pw):
        u = cls.by_name(name)
        if u and valid_pw(name, pw, u.pw_hash):
            return u
xyz
  • 37
  • 6
  • 1
    What is the error you receive? Where are you trying to upload the photo? In one of the Google Cloud Storage services? You need to provide more context information on your application. The question is too broad, please be more specific – Tudormi Apr 23 '18 at 09:39
  • basic: http://blog.notdot.net/2009/9/Handling-file-uploads-in-App-Engine – voscausa Apr 23 '18 at 18:12
  • Sorry for the delay, selecting relevant code and formatting took time. – xyz Apr 24 '18 at 09:12
  • It seems the error you have is `IndexError: list index out of range` for `upload = self.get_uploads()[0]`. So the list might not have any element inside. Check its length before trying to reach the element at index `0` Is the blog entry provided by @voscausa helping you fix the issue? – Tudormi Apr 24 '18 at 10:18
  • @Tudormi how should i check that? I am not familiar with that list and not so bright in this area...@voscausa i need to show that photo in jinja 2 template and we need to store that photo in column which is not shown in that link ...i am really amateur at this so pls if u could guide... – xyz Apr 25 '18 at 05:54

1 Answers1

1

Use blobstore.create_upload_url.

You must set the upload url by a form parameter that you get from the backend in appengine and pass on to the form tag:

  self.render('page',form_url=blobstore.create_upload_url('/upload'
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424