Issue with Website Failing to Redirect to Correct Page After Logging In

Hey there,

I've been working on a website using Python and Flask, and it's been going well so far. But now I've encountered a problem when trying to implement a login feature for the site.

I have set up logic to authenticate users and redirect them to the dashboard upon successful login, but for some reason, it's not working as expected during testing.

I've also added some styles in a style.css file, but I'm wondering if there's a better way to incorporate Bootstrap for a more streamlined design. Any tips on integrating the existing styles with Bootstrap would be greatly appreciated.

Additionally, I've been trying to use flash messaging to display test user credentials, but haven't had any luck getting it to work.

If anyone has any insights or suggestions, please feel free to share.

Login.html

<link rel="stylesheet" href="/static/css/style.css" type="text/css">
{% block body %}
{% if session['logged_in'] %}
<p>You're logged in already!</p>
{% else %}

... (Code continues)

__init__.py

(Python code snippet)

style.css

* {
    box-sizing: border-box;
}

... (CSS code continues)

Answer №1

I implemented hardcoded logic to authenticate users - successful attempts would redirect them to the dashboard, while unsuccessful ones prompt a retry at logging in. However, testing on the website reveals that neither of these actions occur as intended.

The issue likely lies within your code indentation:

def do_admin_login():

error = ''
try:
    if request.method == "POST":
        attempted_email = request.form['email']
        attempted_password = request.form['password']

        flash(attempted_email)
        flash(attempted_password)

        if attempted_password == 'password' and attempted_email == 'admin':
            return redirect(url_for('dashboard'))
        else:
            error = "Invalid credentials. Please try again"
            return render_template('login.html', error = error)

   return render_template('login.html')

except Exception as e:
        return render_template("login.html", error = error)

The first return render_template is triggered by an error condition, while the second one corresponds to a GET request. Also remember to wrap your template name in quotes for render_template

In addition, I've been trying to utilize flash functionality to display test username and password but haven't been able to make it work.

From what I know, you need to pass a string to flash like this: `flash('You have been logged in')

Lastly, although I've created a style.css file, I'm curious if there's a more effective way to achieve the same outcome using Bootstrap. I plan to use Bootstrap for the remainder of the site and wonder about best practices for integrating my styles with the framework.

You can explore flask bootstrap

and update:

def dashboard():
    return ("dashboard.html")

to:

def dashboard():
    return render_template("dashboard.html")

For flashing messages, ensure you do so just before the return statement.

if attempted_password == 'password' and attempted_email == 'admin':
                flash(attempted_password)
                flash(attempted_email)
                return redirect(url_for('dashboard'))
            else:
                error = "Invalid credentials. Please try again"
                return render_template(login.html, error = error)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Newbie's Dilemma: Issues with Html, Email, and Distribution Lists

I am looking to add a new feature to my website where users can enter their email address to receive a free promotion and be added to an announcement list. Since I don't have any experience with web programming, I'm not sure where to start. Would ...

Having trouble aligning my CSS form correctly

The issue is with the alignment of the second row of textbox elements under the "Add Equipment" section on this page: While organizing the code in Dreamweaver, the elements line up correctly. However, when viewed in Google Chrome, they shift to the second ...

Javascript function failing to execute upon page load

I don't have much experience with JavaScript, but I found the following code in a .htm file. The function doesn't seem to be triggering when it should; It is supposed to activate when the page is directly opened i.e www.site.com/12345.htm This ...

How to position an element to the right in Bootstrap 4 without causing it to move to a new line immediately?

As a newcomer to Bootstrap, I have come across only one individual who has raised this issue before. Unfortunately, they did not receive a satisfactory solution, prompting me to bring up the question once more. My dilemma involves two button groups. When ...

What is the reason behind the constraints of min/max width and height values preventing the element from fully filling its parent div?

HTML Code: <div class="content"> <div class="card"> </div> </div> CSS Code: .content { min-height: 350px; min-width: 320px; max-width: 350px; padding: 15px; } .card { width: 100%; height: 100%; ...

Can menus in Vue.js be customized differently for various "pages"?

I am new to Vue and facing a challenge with a common component - menu - that needs to be styled differently on the "TOP page" compared to all other "pages". <template> <div> <menu></menu> <transition name="fade" mode="o ...

Use CSS to place a checked icon within a div containing an image

I have a set of images where clicking on them reveals a green checkmark icon. However, I am struggling to position it correctly. .p-t-md{ padding-top:20px; } .p-l-md{ padding-left:20px; } .speakers_list { width: 605px; margin: 0 auto; height: 245px; ove ...

Positioning a button beside an input field within a table row

This is how my current table layout appears. Table: https://i.sstatic.net/wcHuz.png I am attempting to position the small x button next to the input textbox. Here is the code I have written so far. <table class="table table-hover"> <thead ...

Unable to modify the CSS properties of the titlebar in jQuery UI dialog

Is there a way to modify the background color of a jQuery UI dialog titlebar? Here is the code I am using: jQuery("#divId").dialog({ title: 'Information' }); jQuery("#divId .ui-dialog-titlebar").css("background-color", "red"); For m ...

Adapting CSS styles according to the height of the viewport

Goldman Sachs has an interesting webpage located here. One feature that caught my eye is the header that appears as you scroll down, with squares changing from white to blue based on the section of the page. I'm curious about how they achieved this ef ...

Variations between <div/> and <div></div>

When using Ajax to load two divs, I discovered an interesting difference in the way they are written. If I write them like this: <div id="informCandidacyId"/> <div id="idDivFiles"/> Although both divs are being loaded, only one view is added ...

Divided Progress Indicators in Bootstrap 3

Looking to enhance user experience on my webpage by creating a progress bar for data entry. The goal is for users to enter 10 items before proceeding. The challenge I'm facing is developing a progress bar that accurately reflects the number of items ...

"Interacting with the ng-keypress event causes the page to smoothly scroll

As a newcomer to angularjs, I am trying to figure out why my component (which simulates a "checkbox" using fontawesome icons) causes the page to scroll down when the user presses the space key. Here is a snippet of my component : ugoFmk.component(' ...

When the user presses either the refresh button or the back button, they will be redirected

After the user presses refresh or uses the browser back button, I need to redirect them to a specific page in order to restart the application. My JavaScript code is as follows: var workIsDone = false; window.onbeforeunload = confirmBrowseAway; function ...

Tips for rendering objects in webgl without blending when transparency is enabled

My goal is to display two objects using two separate gl.drawArrays calls. I want any transparent parts of the objects to not be visible. Additionally, I want one object to appear on top of the other so that the first drawn object is hidden where it overlap ...

Font Awesome icons within a nested accordion created using Bootstrap

I have a bootstrap accordion with 3 levels of nesting that is functioning correctly, but I want to make a change in the panel-heading div where I can use a font-awesome icon fa fa-chevron-down when the accordion is open (without affecting the nested accord ...

Increase the Step Size of an HTML Number Input by Holding Down the Time

Is there a way to implement increasing increment/step size for number inputs in HTML based on how long the user holds the stepper arrows? For instance, starting at step size=1 and gradually ramping up to larger increments like 5, 10, 20, etc. after holdin ...

Achieving text alignment with icons in Angular

I'm having trouble aligning my text with the icon next to it despite trying to adjust margins. I would really appreciate any help or suggestions on how to resolve this issue. <div class="notification" [ngClass]="{'no ...

Having issues with jQuery's .fadeIn function not functioning properly

Currently, I'm developing a website and I am aiming to achieve a specific effect where a text fades in every time the webpage is opened. To achieve this, I have implemented the following code snippet: $(document).ready(function() { $('#title ...

Script on Tampermonkey executed prior to page loading

I am facing a challenge with hiding a specific section from an HTML page: <h1 data-ng-show="!menuPinned &amp;&amp; !isSaaS" class="logo floatLeft" aria-hidden="false"><span>XXX&nbsp;</span><span style="font-weight: bold;"& ...