1

Im very much new to Flask, and one of the starting requirements is that i need SEO friendly urls.
I have a route, say

@app.route('/sales/')
@app.route(/sales/<address>)
def get_sales(addr):
  # do some magic here
  # render template of sales

and a simple GET form that submits an address.

<form action={{ url_for('get_sales') }}> 
 <input type='text' name='address'>
 <input type=submit>
</form>

On form submission, the request goes to /sales/?address=somevalue and not to the standard route. What options do I have to have that form submit to /sales/somevalue ? I feel like I'm missing something very basic.

3 Answers 3

3

You would need to use JavaScript to achieve this so your template would become:

<input type='text' id='address'>
 <button onclick="sendUrl();">submit</button>


<script>
    function sendUrl(){
        window.location.assign("/sales/"+document.getElementById("address").value);
    }
</script>

and your routes similar to before:

@app.route('/sales/')
@app.route('/sales/<address>')
def get_sales(address="Nowhere"):
  # do some magic here
  # render template of sales
  return "The address is "+address

However, this is not the best way of doing this kind of thing. An alternative approach is to have flask serve data and use a single-page-application framework in javascript to deal with the routes from a user interface perspective.

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

3 Comments

Can't use the single page app approach due to SEO issues. I'm considering using your custom submit event handler, does this have any negative impact on SEO?
As far as I'm aware, search engine bots will follow links within your site to crawl it. The action of forms will have no impact. I'd avoid going for more technically challenging solutions just to satisfy a perceived SEO benefit. The top search engine is very good now at valuing content over structure. Every hour you spend hacking around server-side routes for SEO would be better spent generating content IMHO.
Accepted, since it does not involve any redirect overhead, and has no SEO impact.
1

There is a difference between the request made when the form is submitted and the response returned. Leave the query string as is, as that is the normal way to interact with a form. When you get a query, process it then redirect to the url you want to display to the user.

@app.route('/sales')
@app.route('/sales/<address>')
def sales(address=None):
    if 'address' in request.args:
        # process the address
        return redirect(url_for('sales', address=address_url_value)

    # address wasn't submitted, show form and address details

Comments

0

I'm not sure there's a way to access the query string like that. The route decorators only work on the base url (minus the query string)

If you want the address in your route handler then you can access it like this:

request.args.get('address', None)

and your route handler will look more like:

@pp.route('/sales')
def get_sales():
    address = request.args.get('address', None)

But if I were to add my 2 cents, you may want to use POST as the method for your form posting. It makes it easier to semantically separate getting data from the Web server (GET) and sending data to the webserver (POST) :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.