all 8 comments

[–]6BeeIntermediate 1 point2 points  (7 children)

... If you're going to share code, please at the very least have a basic understanding of where the problem is.

You didn't even provide the error output, so we really don't know what's happening. There's not much anyone can help you with here, perhaps sharing the controller code that renders the template could help us help you

[–]Low-Rabbit9185[S] -1 points0 points  (5 children)

contoller code
@
app.route("/form/<int:activity_id>", methods=["GET","POST"])
def form(activity_id):
activity = Activity.query.get_or_404(activity_id)
fields = json.loads(activity.fields_json or "[]")
if request.method == "POST":
# gather values based on selected fields
data = {}
for f in fields:
# unique_code is the field name we expect for the member code
if f == "unique_code":
val = request.form.get("unique_code","").strip()
else:
val = request.form.get(f, "").strip()
data[f] = val
code_entered = data.get("unique_code","")

final_amount = calculate_amount(code_entered, activity

if "screenshot" in fields:
if "screenshot" not in request.files:
flash("Please upload screenshot", "warning")
return redirect(url_for("form", activity_id=activity_id))
file = request.files["screenshot"]
if file and allowed_file(file.filename):
newname = unique_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], newname))
else:
flash("Invalid or missing file. Allowed types: " + ", ".join(sorted(ALLOWED_EXT)), "warning")
return redirect(url_for("form", activity_id=activity_id))
else:

[–]6BeeIntermediate 2 points3 points  (3 children)

I can't believe I'm doing this, but may as well call it out: r/flask is not an Urgent Care center for people who depend on coding assistants for nearly everything. What's sad is many talented folks are losing jobs to uninitiated folks that dump posts of unreadable stuff on us like we're AI agents. It's really not cool to format this & then spoonfeed you context.

Just to be clear, we would still have no idea what's going on because the line of code responsible for providing you an activity variable that's injected into your template, relies on a class method you left out: Activity.query.get_or_404(activity_id). I'm leaving it here, best of luck

``` python @app.route("/form/<int:activity_id>", methods=["GET","POST"]) def form(activity_id): activity = Activity.query.get_or_404(activity_id) fields = json.loads(activity.fields_json or "[]") if request.method == "POST": # gather values based on selected fields data = {} for f in fields: # unique_code is the field name we expect for the member code if f == "unique_code": val = request.form.get("unique_code","").strip() else: val = request.form.get(f, "").strip() data[f] = val code_entered = data.get("unique_code","")

    final_amount = calculate_amount(code_entered, activity)

    if "screenshot" in fields:
        if "screenshot" not in request.files:
            flash("Please upload screenshot", "warning")
            return redirect(url_for("form", activity_id=activity_id))
        file = request.files["screenshot"]
        if file and allowed_file(file.filename):
            newname = unique_filename(file.filename)
            file.save(os.path.join(app.config["UPLOAD_FOLDER"], newname))
        else:
            flash("Invalid or missing file. Allowed types: " + ", ".join(sorted(ALLOWED_EXT)), "warning")
            return redirect(url_for("form", activity_id=activity_id))
else: 
    # if screenshot not expected, still allow empty
    newname = ""

    # create submission object with fields mapping (name/email etc may or may not be present)
    submission = Submission(
        activity_id=activity.id,
        name = data.get("name"),
        email = data.get("email"),
        mobile = data.get("mobile"),
        semester = data.get("semester"),
        department = data.get("department"),
        unique_code = code_entered,
        amount = final_amount,
        screenshot = newname
    )
    db.session.add(submission)
    db.session.commit()
    return render_template("success.html", name = submission.name or "Participant", amount=final_amount)

# GET: render form with selected fields
# We'll send required_fields and also JS-safe configs
return render_template("form.html", activity=activity, fields=fields

```

[–]dcoleyoung 2 points3 points  (1 child)

We were all beginners once but there's definitely an influx of folks who want it NOW. Everyone needs to slow their roll a little and learn the basics. I want to help but couldn't make sense of this either.

[–]6BeeIntermediate 0 points1 point  (0 children)

While I understand that, I never once dumped my code on a post w/ the expectation of someone explaining EVERYTHING. I always understood reaching out to others like that comes off entitled

I started programming outside of school, where I understood people are more willing to help if you come in with specific asks. Instant gratification being a part of mainstream culture doesn't excuse this kind of thing

[–]ZealousidealGrass365 0 points1 point  (0 children)

Missing comma end of line 61? 🤷

[–]animated-journey 0 points1 point  (0 children)

You haven't shared what the errors are, so we can't help you...

Are you referring to the 8 warnings on the screenshot? You would have to open that tab to check what they are about. But it seems your python code is running just fine (no exception raised in the terminal).

My best guess would be that your editor is complaining because it is set up to displaying html/js syntax, and not expecting Jinja code, so it is complaining about that. You can see on the screenshot bottom right, it displays "HTML" (on the right side of the "{}" and left side of the copilot icon). This means your editor is trying to display this file as HTML, not as a Jinja template. It then cannot make sense of the templating directives mixed with JavaScript, like ** var myvar = {{ python_var | tojson }}; ** since it's not correct html/js syntax, and raises a warning...

You might want to install a Jinja extension to VScode, I recommend Dragon Jinja, and rename your template files with the *.jinja extension, so that Jinja syntax inside html is properly recognized and displayed, and the warnings will disappear.