繁体   English   中英

python无法将数据推送到mongoDB

[英]python not able to push data to mongoDB

下面的代码采用html注册表格数据并将其推入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)尽管我已经提到数据库名称,服务器地址和集合,但是我看不到mongo中的数据,但我的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加密了密码,同时在密码字段中插入了文本,但密码仍然是纯文本。 不知道我是否在这里缺少任何东西。

好的,我已经解决了一些问题。 我尝试将mLab(Web应用程序)与该python代码集成在一起,现在我可以看到名称为“ users”且用户名/密码以Json格式存储在其中的新集合。 但是为什么代码无法连接到我的mongo安装的本地主机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM