2017-06-01 14 views
0

以下のコードは、html登録フォームのデータをmongoDBにプッシュします。pythonはmongoDBにデータをプッシュできません

from flask import Flask 
from flask import request 
from flask import render_template, flash,redirect,url_for,session,logging 
from flask_pymongo import PyMongo 
from wtforms import Form, StringField, TextAreaField,PasswordField,validators 
from passlib.hash import sha256_crypt 
import bcrypt 

app = Flask(__name__) 
app.config['MONGO_NAME'] = 'amitesh_DB' 
app.config['MONGO_HOST'] = 'mongodb://localhost:27017' 

mongo = PyMongo(app) 

@app.route('/') 
def index(): 
    if 'username' in session: 
     return 'you are logged in as' + session['username'] # gives the message and the name of the username that is logged in that session. 
    return render_template('index.html') 

@app.route('/login') 
def login(): 
    #users = mongo.db.users 
    # users.find_one({'name' : request.form['username']}) 
    return '' 

#we are checking if the username that we are registering doesn't exist, if it does, it will return index.html 

@app.route('/register', methods=['POST','GET']) 
def register(): 

    if request.method == 'POST': 
     users = mongo.db.users #connecting the collection called "users" in mongoDB 
     existing_user = users.find_one({'name':request.form['username']})# we are looking for a name in the mongoDB in the collection "users" that is a username that we are registering with. 

     if existing_user is None:#if the above request.form doesn't find the username with a name that we enter in the html form, then this "if" will allow the new username to register. 
      hashpass = bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt()) 
      users.insert({'name': request.form['username'],'password' : hashpass}) 
      session['username'] = request.form['username'] 
      return redirect(url_for('index')) 

     return 'username already exist' # means, "existing_users" wasn't none 
    return render_template('register1.html') 

if __name__ == '__main__': 
    app.secret_key = 'mysecret' 
    app.run(debug=True) 

私はDBの名前を言及しているにもかかわらず、質問::

1)、サーバアドレス、およびコレクションの下にありますが、私はモンゴ内のデータが表示されていない、私はMongoが動作している、と私のPythonコードは、以下の証拠として、それに通信できるのmongo画面からいくつかのメッセージ::

[thread1] connection accepted from 127.0.0.1:49849 #1 (1 connection now open) 
[conn1] received client metadata from 127.0.0.1:49849 conn1: { driver: { name: "PyMongo", version: "3.4.0" }, os: { type: "Windows", name: "Windows 7", architecture: "AMD64", version: "6.1.7601-SP1" }, platform: "CPython 2.7.12.final.0" } 
[thread1] connection accepted from 127.0.0.1:49850 #2 (2 connections now open) 
[conn2] received client metadata from 127.0.0.1:49850 conn2: { driver: { name: "PyMongo", version: "3.4.0" }, os: { type: "Windows", name: "Windows 7", architecture: "AMD64", version: "6.1.7601-SP1" }, platform: "CPython 2.7.12.final.0" } 
[thread1] connection accepted from 127.0.0.1:49851 #3 (3 connections now open) 
[conn3] received client metadata from 127.0.0.1:49851 conn3: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.4" }, os: { type: "Windows", name: "Microsoft Windows 7", architecture: "x86_64", version: "6.1 SP1 

2)Pythonコードであり、私が提出されたパスワードにテキストを挿入しながら、私のパスワードを暗号化するためにbcryptのを使用していました、それはまだプレーンテキストです。私がここに何かを紛失しているかどうかわからない。

答えて

0

私はいくつかの問題を解決しました。私はmLab(Webアプリケーション)をこのPythonコードと統合しようとしましたが、今度は "users"という名前の新しいコレクションとユーザー名/パスワードがJson形式で格納されていることがわかります。しかし、なぜコードが私のmongoにインストールされたlocalhostに接続できないのですか?

関連する問題