How to make a database connection PAGE/FORM in Django

Viewed 294

Everything I search on this just tries to explain how to connect Django to a database, which is not the goal.

The goal is: I'm trying to make a form where users can insert their database credentials and connect.

I already have it working - but I don't know how to link the users responses to the script that connects to the database.

Here's my form and some of the code (it's Django so code is everywhere):

The db html page:

{% extends 'base.html' %}

{% block content %}
    <h1>New DB Connection</h1>
      <form action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <input class="btn btn-info ml-2" type="button" value="Cancel" onclick="history.back()">
        <button class="btn btn-info ml-2" onclick="location.href='{% url 'output_script' %}'">Connect!</button> <hr>
        {% if df %}
        {{ df }}
        {% endif %}
      </form>
{% endblock content %}

The views.py file: Here's where it gets hard - How do I link the inputs in the DBCreateView class and assign them to the variables in the output function?

class DbCreateView(CreateView):
    # if request.POST:
    #     form = 
    model = db
    template_name = 'db_new.html'
    fields = ('USERNAME','PASSWORD','ACCOUNT','WAREHOUSE','DATABASE', 'SCHEMA', 'ROLE')

#button and database connection
def button(request):
    return render(request,'db_connection.html')
    
def output(request):
    conn = sf.connect(
    account = 'acct_name',
    user = 'username',
    password = 'pw',
    database = 'db',
    warehouse = 'warehouse_name',
    role = 'my_role',
    schema = 'schema_name',
    )

enter image description here

As it is now, if you click connect, it runs a script that queries snowflake and uploads it all back to a new table. But I need the user inputs to the form to be the creds that make it work.

0 Answers
Related