2017-06-01 322 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) 

我有以下問題::

1)雖然我剛纔提到的DB名稱,服務器地址,收藏,但我沒有看到蒙戈的數據,我蒙戈工作,並我的Python代碼可以傳達給它,作爲一個證明,下面是從蒙戈屏幕::

[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安裝的本地主機。