Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is my code @app.route('/retrieveCustomers', methods=['GET', 'POST']) def retrieve_customers(): customers_dict = {} db = shelve.open('customer.db', 'r') customers_dict = db['Customers'] db.close() customers_list = [] for key

this is my code

 @app.route('/retrieveCustomers', methods=['GET', 'POST']) def retrieve_customers(): customers_dict = {} db = shelve.open('customer.db', 'r') customers_dict = db['Customers'] db.close() customers_list = [] for key in customers_dict: customer = customers_dict.get(key) customers_list.append(customer) if request.method == 'POST': sort_by = request.form['sort_by'] gender = request.form['gender'] if sort_by == 'region': customers_list = sorted(customers_list, key=lambda x: x.get_region()) elif sort_by == 'gender': customers_list = [customer for customer in customers_list if customer.get_gender() == gender] return render_template('retrieveCustomers.html', count=len(customers_list), customers_list=customers_list) @app.route('/updateUser//', methods=['GET', 'POST']) def update_user(id): update_user_form = CreateUserForm(request.form) if request.method == 'POST' and update_user_form.validate(): users_dict = {} db = shelve.open('user.db', 'w') users_dict = db['Users'] user = users_dict.get(id) user.set_first_name(update_user_form.first_name.data) user.set_last_name(update_user_form.last_name.data) user.set_gender(update_user_form.gender.data) db['Users'] = users_dict db.close() return redirect(url_for('retrieve_users')) else: users_dict = {} db = shelve.open('user.db', 'r') users_dict = db['Users'] db.close() user = users_dict.get(id) update_user_form.first_name.data = user.get_first_name() update_user_form.last_name.data = user.get_last_name() update_user_form.gender.data = user.get_gender() return render_template('updateUser.html', form=update_user_form) @app.route('/updateCustomer//', methods=['GET', 'POST']) def update_customer(id): update_customer_form = CreateCustomerForm(request.form) if request.method == 'POST' and update_customer_form.validate(): customers_dict = {} db = shelve.open('customer.db', 'w') customers_dict = db['Customers'] customer = customers_dict.get(id) customer.set_first_name(update_customer_form.first_name.data) customer.set_last_name(update_customer_form.last_name.data) customer.set_gender(update_customer_form.gender.data) customer.set_email(update_customer_form.email.data) customer.set_date_joined(update_customer_form.date_joined.data) customer.set_region(update_customer_form.region.data) customer.set_address(update_customer_form.address.data) db['Customers'] = customers_dict db.close() return redirect(url_for('retrieve_customers')) else: customers_dict = {} db = shelve.open('customer.db', 'r') customers_dict = db['Customers'] db.close() customer = customers_dict.get(id) update_customer_form.first_name.data = customer.get_first_name() update_customer_form.last_name.data = customer.get_last_name() update_customer_form.gender.data = customer.get_gender() update_customer_form.email.data = customer.get_email() update_customer_form.date_joined.data = customer.get_date_joined() update_customer_form.region.data = customer.get_region() update_customer_form.address.data = customer.get_address() return render_template('updateCustomer.html', form=update_customer_form) @app.route('/deleteUser/', methods=['POST']) def delete_user(id): users_dict = {} db = shelve.open('user.db', 'w') users_dict = db['Users'] users_dict.pop(id) db['Users'] = users_dict db.close() return redirect(url_for('retrieve_users')) @app.route("/logout") def logout(): return redirect(url_for('login')) @app.route('/deleteCustomer/', methods=['POST']) def delete_customer(id): customers_dict = {} db = shelve.open('customer.db', 'w') customers_dict = db['Customers'] customers_dict.pop(id) db['Customers'] = customers_dict db.close() return redirect(url_for('retrieve_customers')) if __name__ == '__main__': app.run(port=4996) 

the is my html for retrieveCustomer

{% extends "base.html" %} {% block title %}Library Loan System - Retrieve Customers{% endblock %} {% block content %} 

Retrieve Customers

{% if count == 0 %}

There are no customers.

{% elif count == 1 %}

There is 1 customer.

{% else %}

There are {{ count }} customers.

{% endif %}
{% for customer in customers_list %} {% endfor %}
User ID Customer ID First Name Last Name Gender Email Date Joined region Address
{{ customer.get_user_id() }} {{ customer.get_customer_id() }} {{ customer.get_first_name() }} {{ customer.get_last_name() }} {{ customer.get_gender() }} {{ customer.get_email() }} {{ customer.get_date_joined() }} {{ customer.get_region() }} {{ customer.get_address() }} Update

Delete Confirmation

Are you sure you want to delete {{customer.get_first_name()}} {{customer.get_last_name()}}?
{% endblock %}

this is my code i want sort the customer based on gender and region when i tried to code it shows error

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

this is my pycharm error

127.0.0.1 - - [03/Feb/2023 21:54:13] "GET / HTTP/1.1" 200 - [2023-02-03 21:54:16,749] ERROR in app: Exception on /retrieveCustomers [GET] Traceback (most recent call last): File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\flask\app.py", line 2525, in wsgi_app response = self.full_dispatch_request() File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\flask\app.py", line 1796, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "C:\Users\praka\PycharmProjects\simpleweb\__init__.py", line 97, in retrieve_customers return render_template('retrieveCustomers.html', count=len(customers_list), customers_list=customers_list) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\flask\templating.py", line 146, in render_template template = app.jinja_env.get_or_select_template(template_name_or_list) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\jinja2\environment.py", line 1081, in get_or_select_template return self.get_template(template_name_or_list, parent, globals) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\jinja2\environment.py", line 1010, in get_template return self._load_template(name, globals) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\jinja2\environment.py", line 969, in _load_template template = self.loader.load(self, name, self.make_globals(globals)) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\jinja2\loaders.py", line 138, in load code = environment.compile(source, name, filename) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\jinja2\environment.py", line 768, in compile self.handle_exception(source=source_hint) File "C:\Users\praka\PycharmProjects\simpleweb\venv\lib\site-packages\jinja2\environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "C:\Users\praka\PycharmProjects\simpleweb\template etrieveCustomers.html", line 17, in template

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'url_for'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'. 127.0.0.1 - - [03/Feb/2023 21:54:16] "GET /retrieveCustomers HTTP/1.1" 500 -

pls help me trouble shoot my code and help with the solution

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Knowledge Discovery In Databases

Authors: Gregory Piatetsky-Shapiro, William Frawley

1st Edition

0262660709, 978-0262660709

More Books

Students also viewed these Databases questions