Search code examples
pythonauthenticationpyramid

How do I set up a login system in pyramid using mysql as database to store email and password?


I have gone through this http://docs.pylonsproject.org/projects/pyramid/en/latest/quick_tutorial/authentication.html but it does not give any clue how to add database to this to store email and password?


Solution

  • Here are a few suggestions regarding switching from SQLite to MySQL. In your development.ini (and/or production.ini) file, change from SQLite to MySQL:

    # sqlalchemy.url = sqlite:///%(here)s/MyProject.sqlite  [comment out or remove this line]
    sqlalchemy.url = mysql://MySQLUsername:MySQLPassword@localhost/MySQLdbName
    

    Of course, you will need a MySQL database (MySQLdbName in the example above) and likely the knowledge and privileges to edit its metadata, for example, to add fields called user_email and passwordhash to the users table or create a users table if necessary.

    In your setup.py file, you will require the mysql-python module to be imported. An example would be:

    requires = [
        'bcrypt',
        'pyramid',
        'pyramid_jinja2',
        'pyramid_debugtoolbar',
        'pyramid_tm',
        'SQLAlchemy',
        'transaction',
        'zope.sqlalchemy',
        'waitress',
        'mysql-python',
        ]
    

    After specifying new module(s) in setup.py, be sure to run the following commands so your project recognizes the new module(s):

    cd $VENV/MyPyramidProject
    sudo $VENV/bin/pip install -e .
    

    By this point, your Pyramid project should be hooked up to MySQL. Now it is down to learning the details of Pyramid (and SQLAlchemy if this is your selected ORM). Much of the suggestions in the tutorials, partcularly the SQLAlchemy + URL dispatch wiki tutorial in your case, should work as they work with SQLite.