尝试使用html python和谷歌应用引擎上传照片

时间:2018-04-23 07:06:54

标签: python html google-app-engine webapp2 blobstore

有人可以就如何解决问题提出基本想法。它没有在localhost上工作,blobstore也无法正常工作。有没有其他方法或有人能告诉我正确的方法? 我只显示与查询相关的代码.... enter image description here这是错误的图片

html代码

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

从表格中获取输入。

类注册(BlogHandler):     def get(self):         self.render(&#34;注册-form.html&#34)

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

用于将参数传递给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')

用户模型。 传递参数profile_pic并将profile_pic分配给用户实体

中的列profile_pic
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

1 个答案:

答案 0 :(得分:1)

使用public class planning extends Fragment implements DatePickerDialog.OnDateSetListener{ // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public planning() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment planning. */ // TODO: Rename and change types and number of parameters public static planning newInstance(String param1, String param2) { planning fragment = new planning(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_planning, container, false); final Button chooseDate = (Button)view.findViewById(R.id.chooseDate); chooseDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogFragment datePicker = new DatePickerFragment(); datePicker.show(getChildFragmentManager(), "date picker"); } }); return view; } @Override public void onDateSet(DatePicker view, int year, int month, int day) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime()); TextView date = (TextView)view.findViewById(R.id.date); date.setText(currentDateString); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super.onAttach(context); } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } }

您必须使用从appengine后端获取的表单参数设置上传网址,并将其传递给表单标记:

blobstore.create_upload_url