Input a link to submit a form and then extract data from that link – FLASK

I have set up a small section within my application where a user can add a link and submit/apply for a job. I then want to extract information from the link the user inputs and save it within my database. However, I am quite stuck. I can’t seem to get the requests.get to recognise the input because it is a link. Also, I am not sure about how to save the info, within a specific column within the database.

The HTML form is this:

<!DOCTYPE html> {% extends "layout.html" %} {% block content %}
<article class="media content-section">
    <h1 class="article-content">{{ current_post.job_title }}</h1>
    <h5 class="article-content">{{ current_post.company}}</h5>
    <p class="article-content">{{ current_post.sector }}</p>
    <p class="article-content">{{ current_post.location }}</p>
    <p class="article-content">{{ current_post.employment_type }}</p>
    <p class="article-content">{{ current_post.description }}</p>
    <p class="font-weight-bold">Requirements:</p>
    <ul>
        <li class="article-content">{{ current_post.requirement_1 }}</li>
        <li class="article-content">{{ current_post.requirement_2 }}</li>
        <li class="article-content">{{ current_post.requirement_3 }}</li>
        <li class="article-content">{{ current_post.requirement_4 }}</li>
        <li class="article-content">{{ current_post.requirement_5 }}</li>
        <li class="article-content">{{ current_post.requirement_6 }}</li>
    </ul>
    </li>
    <p>To apply for this job please submit your Badgr backpack link</p>
    <form method="POST" class="row g-3" action="/apply">
        <div class="col-auto">
            <label for="url" class="visually-hidden">Backpack Link</label>
            <input type="Link" class="form-control" name="url" id="url" placeholder="Backpack Link">
        </div>
        <div class="col-auto">
            <button type="submit" class="btn btn-primary mb-3">Apply</button>
        </div>
    </form>
</article>
{%endblock content %}

And the part of my py file relevant for this is below:

The info in the variable dataskills is what I want to be saved into the database.

@app.route("/apply", methods=['GET', 'POST'])
@login_required
def apply():
    if request.method == 'POST':
        r = requests.get(url)
        jsondata = r.json()
        listed = []
        for song in jsondata['badges']:
            new = listed.append(song['badge'])
        for b in listed:
            r = requests.get(b)
            text_json = json.loads(r.text)
            dataskills = text_json['name']
        ap = Applicant(skills = dataskills) #skills is the column within the database I want to save info into
        db.session.add(ap)
        db.session.commit()      
        flash('Congratulations you have successfully applied for this job!', 'success')
    return redirect(url_for('jobs'))

Answer

I didn’t understand your code properly but I found your problem, you aren’t getting url from form.

You can do this to get the data from form:

url=request.form["url"]

The syntax is basic, request.form[<string from name>]. In your case <input type="Link" class="form-control" name="url" id="url" placeholder="Backpack Link"> has “url” in name. So, for you code become request.form["url"].

Full code:

@app.route("/apply", methods=['GET', 'POST'])
@login_required
def apply():
    if request.method == 'POST':
        url=request.form["url"]
        r = requests.get(url)
        jsondata = r.json()
        listed = []
        for song in jsondata['badges']:
            new = listed.append(song['badge'])
        for b in listed:
            r = requests.get(b)
            text_json = json.loads(r.text)
            dataskills = text_json['name']
        ap = Applicant(skills = dataskills) #skills is the column within the database I want to save info into
        db.session.add(ap)
        db.session.commit()      
        flash('Congratulations you have successfully applied for this job!', 'success')
    return redirect(url_for('jobs'))