0

I'm writing a small application in Flask.

run.py

#!flask/bin/python from app import app app.run(debug=True, port=9001) 

init.py

from flask import Flask app = Flask(__name__) from app import views 

index.html

{% extends "base.html" %} {% block content %} <select id = "foo"> {% for item in Citydata %} <option value = {{ item.link }}> {{ item.name }} </option> {% endfor %} </select> <a href="/new">Click here</a> {% endblock %} 

new.html

{% extends "base.html" %} {% block content %} <p>gafgafgadfgaerwgtdfzgaergdfzgaergaergaergt</p> {% endblock %} 

and lastly views.py

from flask import render_template from app import app from bs4 import BeautifulSoup import urllib2 import traceback class City_Link(object): name = "" link = "" # The class "constructor" - It's actually an initializer def __init__(self, name, link): self.name = name self.link = link @app.route('/') @app.route('/index') def index(): URL = 'http://www.amis.pk/DistrictCities.aspx' City_Data = scrape(URL) return render_template("index.html", title='Home', Citydata=City_Data) @app.route('/new/<data>', methods=['GET', 'POST']) def new(data): return render_template("new.html", title='Home', link = data) def scrape(url): data = [] try: page = urllib2.urlopen(url) soup = BeautifulSoup(page.read(), "lxml") table = soup.body.find(id='TABLE1') for row in table.findAll("tr"): heads = row.findAll("a") for head in heads: data.append((City_Link(head.text.strip(), head.attrs['href']))) except: print(traceback.format_exc()) return data 

When i click on the "Click me" href in index.html It gives me a 404 not found on the template new.html. I dont understand why because I followed a basic tutorial. I tried changing ports and it worked. But then I tried to update the code and it broke the link again.

1 Answer 1

1

So the reason this happens is because flask registers /new and /new/ to be two different routes.

It also looks like you're not actually passing in any data to the data variable anyway. You can temporarily fix this issue by changing your link to point to

/new/something 

But that doesn't totally address the problem. I recommend adjusting your template code to make use of flasks excellent url_for function. You can find the extended documentation for it here: http://flask.pocoo.org/docs/0.10/api/#flask.url_for

When we adjust your code, it should look like:

<a href="{{ url_for('new') }}">Click here</a> 

And that data variable looks like it's not even used! Let's just strip it out totally!

@app.route('/new', methods=['GET', 'POST']) def new(): return render_template("new.html", title='Home') 

This has altered your code, and I may not have enough information about your use case. If this modifies it beyond being usable for your application, let me know and I'll adjust my answer accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.