librelist archives

« back to archive

flask render_template url for index.html#contact

flask render_template url for index.html#contact

From:
Uday Choudhary
Date:
2013-08-05 @ 03:49
Hello,

My flask app has single index.html page which has <div class="main">
 <div class="contact"> etc. in same page but they are shown as different
pages into the browsers. Its just css trick to show sliders etc. using a
single page.

Now i have *index.html#main* content page and a *index.html#contact* page
which has a form through which i want to use this logic.

*if request.method == 'POST':*
* if form.validate() == False:*
* flash('All fields are required.')*
* return render_template('index.html', form = form)*
*
*
But here i want to render_template('index.html#contact') so that it shows
#contact part of the index.html template.

How can flask/Jinja render <div class="contact"> contact content</div> in
the index.html page?

Regards

Re: [flask] flask render_template url for index.html#contact

From:
Bruno Rocha
Date:
2013-08-05 @ 04:06
Not that way. Better is to use a base page and extend it.

# templates/base.html
<html>
<head>......</head>
<body>
{% block content %}
{% endblock %}
</body>

# templates/main.html
{% extends 'base.html' %}
{% block content %}
  Here you have your main page
{% endblock %}

# templates/contact.html
{% extends 'base.html' %}
{% block content %}
   <form>
      Your contact form
   </form>
{% endblock %}
</html>

Then in your app

app = Flask(__nane__)
...
...

@app.route("/")
@app.route("/main")
def main():
   return render_template("main.html")


@app.route("/contact")
def contact():
    form = "build your form"
*    if request.method == 'POST':*
*        if form.validate() == False:*
* flash('All fields are required.')*
   return render_template("contact.html", form=form)


If you need to do something different, then you have to render just one
template and rely only on CSS/Javascript, or maybe deal with that separate
endpoints via ajax calls.




On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com> wrote:

> Hello,
>
> My flask app has single index.html page which has <div class="main">
>  <div class="contact"> etc. in same page but they are shown as different
> pages into the browsers. Its just css trick to show sliders etc. using a
> single page.
>
> Now i have *index.html#main* content page and a *index.html#contact* page
> which has a form through which i want to use this logic.
>
> *if request.method == 'POST':*
> * if form.validate() == False:*
> * flash('All fields are required.')*
> * return render_template('index.html', form = form)*
> *
> *
> But here i want to render_template('index.html#contact') so that it shows
> #contact part of the index.html template.
>
> How can flask/Jinja render <div class="contact"> contact content</div> in
> the index.html page?
>
> Regards
>

回复: [flask] flask render_template url for index.html#contact

From:
Robin Deng
Date:
2013-08-05 @ 04:20
ajax is a good way.   

if you real want "index.html#main" ,  try  url_for() :

@app.route("/index")
def index:
    ….
    return redirect(url_for('index')+'#main')


--  
Robin Deng

已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  

在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:

> Not that way. Better is to use a base page and extend it.
>  
> # templates/base.html
> <html>
> <head>......</head>
> <body>
> {% block content %}
> {% endblock %}
> </body>
>  
> # templates/main.html
> {% extends 'base.html' %}
> {% block content %}
>   Here you have your main page
> {% endblock %}
>  
> # templates/contact.html
> {% extends 'base.html' %}
> {% block content %}
>    <form>
>       Your contact form  
>    </form>
> {% endblock %}
>  
> </html>
>  
> Then in your app
>  
> app = Flask(__nane__)
> ...
> ...
>  
> @app.route("/")
> @app.route("/main")
> def main():
>    return render_template("main.html")
>  
>  
> @app.route("/contact")
> def contact():
>     form = "build your form"
>     if request.method == 'POST':
>         if form.validate() == False:
> flash('All fields are required.')
>  
>    return render_template("contact.html", form=form)
>  
>  
> If you need to do something different, then you have to render just one 
template and rely only on CSS/Javascript, or maybe deal with that separate
endpoints via ajax calls.  
>  
>  
>  
>  
> On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com 
(mailto:mobulite@gmail.com)> wrote:
> > Hello,
> >  
> > My flask app has single index.html page which has <div class="main">  
> >  <div class="contact"> etc. in same page but they are shown as 
different pages into the browsers. Its just css trick to show sliders etc.
using a single page.
> >  
> > Now i have index.html#main content page and a index.html#contact page 
which has a form through which i want to use this logic.
> >  
> > if request.method == 'POST':  
> > if form.validate() == False:
> > flash('All fields are required.')
> > return render_template('index.html', form = form)
> >  
> >  
> > But here i want to render_template('index.html#contact') so that it 
shows #contact part of the index.html template.
> >  
> > How can flask/Jinja render <div class="contact"> contact content</div>
in the index.html page?  
> >  
> > Regards  

Re: 回复: [flask] flask render_template url for

From:
Uday Choudhary
Date:
2013-08-05 @ 04:43
Hello Robin,

As you suggested, return redirect(url_for('index')+'#main') works fine. But
it reloads website.

But i also need to pass some data which so i tried: *return
redirect(url_for('index')+'#contact', success=True)* but it didn't worked.

I tried *return render_template(url_for('index')+'#contact**')* but i got
error.* TemplateNotFound: /#contact*
worked.

Isn't there any way to do something like this : *
return render_template(url_for('index')+'#contact**') ? *or a way to pass
data along with #contact trick?
*
*
Regards


On Mon, Aug 5, 2013 at 9:50 AM, Robin Deng <gl.deng@gmail.com> wrote:

> ajax is a good way.
>
> if you real want "*index.html#main*" ,  try  url_for() :
>
> @app.route("/index")
> def index:
>     ….
>     return redirect(url_for('index')+'#main')
>
> --
> Robin Deng
>
> 已使用 Sparrow <http://www.sparrowmailapp.com/?sig>
>
> 在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:
>
> Not that way. Better is to use a base page and extend it.
>
> # templates/base.html
> <html>
> <head>......</head>
> <body>
> {% block content %}
> {% endblock %}
> </body>
>
> # templates/main.html
> {% extends 'base.html' %}
> {% block content %}
>   Here you have your main page
> {% endblock %}
>
> # templates/contact.html
> {% extends 'base.html' %}
> {% block content %}
>    <form>
>       Your contact form
>    </form>
> {% endblock %}
> </html>
>
> Then in your app
>
> app = Flask(__nane__)
> ...
> ...
>
> @app.route("/")
> @app.route("/main")
> def main():
>    return render_template("main.html")
>
>
> @app.route("/contact")
> def contact():
>     form = "build your form"
> *    if request.method == 'POST':*
> *        if form.validate() == False:*
> * flash('All fields are required.')*
>    return render_template("contact.html", form=form)
>
>
> If you need to do something different, then you have to render just one
> template and rely only on CSS/Javascript, or maybe deal with that separate
> endpoints via ajax calls.
>
>
>
>
> On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com>wrote:
>
> Hello,
>
> My flask app has single index.html page which has <div class="main">
>  <div class="contact"> etc. in same page but they are shown as different
> pages into the browsers. Its just css trick to show sliders etc. using a
> single page.
>
> Now i have *index.html#main* content page and a *index.html#contact* page
> which has a form through which i want to use this logic.
>
> *if request.method == 'POST':*
> * if form.validate() == False:*
> * flash('All fields are required.')*
> * return render_template('index.html', form = form)*
> *
> *
> But here i want to render_template('index.html#contact') so that it shows
> #contact part of the index.html template.
>
> How can flask/Jinja render <div class="contact"> contact content</div> in
> the index.html page?
>
> Regards
>
>
>
>

回复: [flask] flask render_template url for

From:
Robin Deng
Date:
2013-08-05 @ 04:53
if you just send data as simple as "success=True"。  you can send it by  
hash  url_for('index')+'?success=true#contact' .

but this seem's not the best way.

waiting for others...  

--  
Robin Deng

已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  

在 2013年8月5日星期一,下午12:43,Uday Choudhary 写道:

> Hello Robin,
>  
> As you suggested, return redirect(url_for('index')+'#main') works fine. 
But it reloads website.  
>  
> But i also need to pass some data which so i tried: return 
redirect(url_for('index')+'#contact', success=True) but it didn't worked.

>  
> I tried return render_template(url_for('index')+'#contact') but i got 
error. TemplateNotFound: /#contact  
> worked.
>  
> Isn't there any way to do something like this : return 
render_template(url_for('index')+'#contact') ? or a way to pass data along
with #contact trick?  
>  
> Regards
>  
>  
> On Mon, Aug 5, 2013 at 9:50 AM, Robin Deng <gl.deng@gmail.com 
(mailto:gl.deng@gmail.com)> wrote:
> > ajax is a good way.   
> >  
> > if you real want "index.html#main" ,  try  url_for() :
> >  
> > @app.route("/index")  
> > def index:
> >     ….
> >     return redirect(url_for('index')+'#main')
> >  
> >  
> > --  
> > Robin Deng
> >  
> > 已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  
> >  
> > 在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:
> >  
> > > Not that way. Better is to use a base page and extend it.
> > >  
> > > # templates/base.html
> > > <html>
> > > <head>......</head>
> > > <body>
> > > {% block content %}
> > > {% endblock %}
> > > </body>
> > >  
> > > # templates/main.html
> > > {% extends 'base.html' %}
> > > {% block content %}
> > >   Here you have your main page
> > > {% endblock %}
> > >  
> > > # templates/contact.html
> > > {% extends 'base.html' %}
> > > {% block content %}
> > >    <form>
> > >       Your contact form  
> > >    </form>
> > > {% endblock %}
> > >  
> > > </html>
> > >  
> > > Then in your app
> > >  
> > > app = Flask(__nane__)
> > > ...
> > > ...
> > >  
> > > @app.route("/")
> > > @app.route("/main")
> > > def main():
> > >    return render_template("main.html")
> > >  
> > >  
> > > @app.route("/contact")
> > > def contact():
> > >     form = "build your form"
> > >     if request.method == 'POST':
> > >         if form.validate() == False:
> > > flash('All fields are required.')
> > >  
> > >    return render_template("contact.html", form=form)
> > >  
> > >  
> > > If you need to do something different, then you have to render just 
one template and rely only on CSS/Javascript, or maybe deal with that 
separate endpoints via ajax calls.  
> > >  
> > >  
> > >  
> > >  
> > > On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com 
(mailto:mobulite@gmail.com)> wrote:
> > > > Hello,
> > > >  
> > > > My flask app has single index.html page which has <div class="main">  
> > > >  <div class="contact"> etc. in same page but they are shown as 
different pages into the browsers. Its just css trick to show sliders etc.
using a single page.
> > > >  
> > > > Now i have index.html#main content page and a index.html#contact 
page which has a form through which i want to use this logic.
> > > >  
> > > > if request.method == 'POST':  
> > > > if form.validate() == False:
> > > > flash('All fields are required.')
> > > > return render_template('index.html', form = form)
> > > >  
> > > >  
> > > > But here i want to render_template('index.html#contact') so that 
it shows #contact part of the index.html template.
> > > >  
> > > > How can flask/Jinja render <div class="contact"> contact 
content</div> in the index.html page?  
> > > >  
> > > > Regards  
> >  
>  

回复: [flask] flask render_template url for

From:
Robin Deng
Date:
2013-08-05 @ 04:55
er, not 'hash'  but  'query string'

--  
Robin Deng

已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  

在 2013年8月5日星期一,下午12:53,Robin Deng 写道:

> if you just send data as simple as "success=True"。  you can send it by  
hash  url_for('index')+'?success=true#contact' .
>  
> but this seem's not the best way.
>  
> waiting for others...  
>  
> --  
> Robin Deng
>  
> 已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  
>  
> 在 2013年8月5日星期一,下午12:43,Uday Choudhary 写道:
>  
> > Hello Robin,
> >  
> > As you suggested, return redirect(url_for('index')+'#main') works 
fine. But it reloads website.  
> >  
> > But i also need to pass some data which so i tried: return 
redirect(url_for('index')+'#contact', success=True) but it didn't worked.

> >  
> > I tried return render_template(url_for('index')+'#contact') but i got 
error. TemplateNotFound: /#contact  
> > worked.
> >  
> > Isn't there any way to do something like this : return 
render_template(url_for('index')+'#contact') ? or a way to pass data along
with #contact trick?  
> >  
> > Regards
> >  
> >  
> > On Mon, Aug 5, 2013 at 9:50 AM, Robin Deng <gl.deng@gmail.com 
(mailto:gl.deng@gmail.com)> wrote:
> > > ajax is a good way.   
> > >  
> > > if you real want "index.html#main" ,  try  url_for() :
> > >  
> > > @app.route("/index")  
> > > def index:
> > >     ….
> > >     return redirect(url_for('index')+'#main')
> > >  
> > >  
> > > --  
> > > Robin Deng
> > >  
> > > 已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  
> > >  
> > > 在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:
> > >  
> > > > Not that way. Better is to use a base page and extend it.
> > > >  
> > > > # templates/base.html
> > > > <html>
> > > > <head>......</head>
> > > > <body>
> > > > {% block content %}
> > > > {% endblock %}
> > > > </body>
> > > >  
> > > > # templates/main.html
> > > > {% extends 'base.html' %}
> > > > {% block content %}
> > > >   Here you have your main page
> > > > {% endblock %}
> > > >  
> > > > # templates/contact.html
> > > > {% extends 'base.html' %}
> > > > {% block content %}
> > > >    <form>
> > > >       Your contact form  
> > > >    </form>
> > > > {% endblock %}
> > > >  
> > > > </html>
> > > >  
> > > > Then in your app
> > > >  
> > > > app = Flask(__nane__)
> > > > ...
> > > > ...
> > > >  
> > > > @app.route("/")
> > > > @app.route("/main")
> > > > def main():
> > > >    return render_template("main.html")
> > > >  
> > > >  
> > > > @app.route("/contact")
> > > > def contact():
> > > >     form = "build your form"
> > > >     if request.method == 'POST':
> > > >         if form.validate() == False:
> > > > flash('All fields are required.')
> > > >  
> > > >    return render_template("contact.html", form=form)
> > > >  
> > > >  
> > > > If you need to do something different, then you have to render 
just one template and rely only on CSS/Javascript, or maybe deal with that
separate endpoints via ajax calls.  
> > > >  
> > > >  
> > > >  
> > > >  
> > > > On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary 
<mobulite@gmail.com (mailto:mobulite@gmail.com)> wrote:
> > > > > Hello,
> > > > >  
> > > > > My flask app has single index.html page which has <div class="main">  
> > > > >  <div class="contact"> etc. in same page but they are shown as 
different pages into the browsers. Its just css trick to show sliders etc.
using a single page.
> > > > >  
> > > > > Now i have index.html#main content page and a index.html#contact
page which has a form through which i want to use this logic.
> > > > >  
> > > > > if request.method == 'POST':  
> > > > > if form.validate() == False:
> > > > > flash('All fields are required.')
> > > > > return render_template('index.html', form = form)
> > > > >  
> > > > >  
> > > > > But here i want to render_template('index.html#contact') so that
it shows #contact part of the index.html template.
> > > > >  
> > > > > How can flask/Jinja render <div class="contact"> contact 
content</div> in the index.html page?  
> > > > >  
> > > > > Regards  
> > >  
> >  
>  

回复: [flask] flask render_template url for

From:
Robin Deng
Date:
2013-08-05 @ 05:09
i hava an idea:

also, use :

return render_template("contact.html", form=form)

and send an extra data to template  

return render_template("contact.html", form=form, pos='#4')

then, you can get this 'pos'  by js in template, and scroll page to the 
right position

ps:  jquery ScrollTo plug: http://demos.flesler.com/jquery/scrollTo/  

--  
Robin Deng

已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  

在 2013年8月5日星期一,下午12:55,Robin Deng 写道:

> er, not 'hash'  but  'query string'
>  
> --  
> Robin Deng
>  
> 已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  
>  
> 在 2013年8月5日星期一,下午12:53,Robin Deng 写道:
>  
> > if you just send data as simple as "success=True"。  you can send it by
hash  url_for('index')+'?success=true#contact' .
> >  
> > but this seem's not the best way.
> >  
> > waiting for others...  
> >  
> > --  
> > Robin Deng
> >  
> > 已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  
> >  
> > 在 2013年8月5日星期一,下午12:43,Uday Choudhary 写道:
> >  
> > > Hello Robin,
> > >  
> > > As you suggested, return redirect(url_for('index')+'#main') works 
fine. But it reloads website.  
> > >  
> > > But i also need to pass some data which so i tried: return 
redirect(url_for('index')+'#contact', success=True) but it didn't worked.

> > >  
> > > I tried return render_template(url_for('index')+'#contact') but i 
got error. TemplateNotFound: /#contact  
> > > worked.
> > >  
> > > Isn't there any way to do something like this : return 
render_template(url_for('index')+'#contact') ? or a way to pass data along
with #contact trick?  
> > >  
> > > Regards
> > >  
> > >  
> > > On Mon, Aug 5, 2013 at 9:50 AM, Robin Deng <gl.deng@gmail.com 
(mailto:gl.deng@gmail.com)> wrote:
> > > > ajax is a good way.   
> > > >  
> > > > if you real want "index.html#main" ,  try  url_for() :
> > > >  
> > > > @app.route("/index")  
> > > > def index:
> > > >     ….
> > > >     return redirect(url_for('index')+'#main')
> > > >  
> > > >  
> > > > --  
> > > > Robin Deng
> > > >  
> > > > 已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  
> > > >  
> > > > 在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:
> > > >  
> > > > > Not that way. Better is to use a base page and extend it.
> > > > >  
> > > > > # templates/base.html
> > > > > <html>
> > > > > <head>......</head>
> > > > > <body>
> > > > > {% block content %}
> > > > > {% endblock %}
> > > > > </body>
> > > > >  
> > > > > # templates/main.html
> > > > > {% extends 'base.html' %}
> > > > > {% block content %}
> > > > >   Here you have your main page
> > > > > {% endblock %}
> > > > >  
> > > > > # templates/contact.html
> > > > > {% extends 'base.html' %}
> > > > > {% block content %}
> > > > >    <form>
> > > > >       Your contact form  
> > > > >    </form>
> > > > > {% endblock %}
> > > > >  
> > > > > </html>
> > > > >  
> > > > > Then in your app
> > > > >  
> > > > > app = Flask(__nane__)
> > > > > ...
> > > > > ...
> > > > >  
> > > > > @app.route("/")
> > > > > @app.route("/main")
> > > > > def main():
> > > > >    return render_template("main.html")
> > > > >  
> > > > >  
> > > > > @app.route("/contact")
> > > > > def contact():
> > > > >     form = "build your form"
> > > > >     if request.method == 'POST':
> > > > >         if form.validate() == False:
> > > > > flash('All fields are required.')
> > > > >  
> > > > >    return render_template("contact.html", form=form)
> > > > >  
> > > > >  
> > > > > If you need to do something different, then you have to render 
just one template and rely only on CSS/Javascript, or maybe deal with that
separate endpoints via ajax calls.  
> > > > >  
> > > > >  
> > > > >  
> > > > >  
> > > > > On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary 
<mobulite@gmail.com (mailto:mobulite@gmail.com)> wrote:
> > > > > > Hello,
> > > > > >  
> > > > > > My flask app has single index.html page which has <div class="main">  
> > > > > >  <div class="contact"> etc. in same page but they are shown as
different pages into the browsers. Its just css trick to show sliders etc.
using a single page.
> > > > > >  
> > > > > > Now i have index.html#main content page and a 
index.html#contact page which has a form through which i want to use this 
logic.
> > > > > >  
> > > > > > if request.method == 'POST':  
> > > > > > if form.validate() == False:
> > > > > > flash('All fields are required.')
> > > > > > return render_template('index.html', form = form)
> > > > > >  
> > > > > >  
> > > > > > But here i want to render_template('index.html#contact') so 
that it shows #contact part of the index.html template.
> > > > > >  
> > > > > > How can flask/Jinja render <div class="contact"> contact 
content</div> in the index.html page?  
> > > > > >  
> > > > > > Regards  
> > > >  
> > >  
> >  
>  

Re: 回复: [flask] flask render_template url for

From:
Uday Choudhary
Date:
2013-08-05 @ 05:02
hello Robin,

so another url *redirect(url_for('index')+'#contact', form = form)* should
be *redirect(url_for('index')+'?form=form#contact')* ?

Regards


On Mon, Aug 5, 2013 at 10:25 AM, Robin Deng <gl.deng@gmail.com> wrote:

> er, not 'hash'  but  'query string'
>
> --
> Robin Deng
>
> 已使用 Sparrow <http://www.sparrowmailapp.com/?sig>
>
> 在 2013年8月5日星期一,下午12:53,Robin Deng 写道:
>
>  if you just send data as simple as "*success=True*"。  you can send it by
>  hash  *url_for('index')+'?success=true#contact**' .*
> *
> *
> *but this seem's not the best way.*
> *
> *
> *waiting for others...*
>
> --
> Robin Deng
>
> 已使用 Sparrow <http://www.sparrowmailapp.com/?sig>
>
> 在 2013年8月5日星期一,下午12:43,Uday Choudhary 写道:
>
> Hello Robin,
>
> As you suggested, return redirect(url_for('index')+'#main') works fine.
> But it reloads website.
>
> But i also need to pass some data which so i tried: *return
> redirect(url_for('index')+'#contact', success=True)* but it didn't worked.
>
> I tried *return render_template(url_for('index')+'#contact**')* but i got
> error.* TemplateNotFound: /#contact*
> worked.
>
> Isn't there any way to do something like this : *
> return render_template(url_for('index')+'#contact**') ? *or a way to pass
> data along with #contact trick?
> *
> *
> Regards
>
>
> On Mon, Aug 5, 2013 at 9:50 AM, Robin Deng <gl.deng@gmail.com> wrote:
>
> ajax is a good way.
>
> if you real want "*index.html#main*" ,  try  url_for() :
>
> @app.route("/index")
> def index:
>     ….
>     return redirect(url_for('index')+'#main')
>
> --
> Robin Deng
>
> 已使用 Sparrow <http://www.sparrowmailapp.com/?sig>
>
> 在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:
>
> Not that way. Better is to use a base page and extend it.
>
> # templates/base.html
> <html>
> <head>......</head>
> <body>
> {% block content %}
> {% endblock %}
> </body>
>
> # templates/main.html
> {% extends 'base.html' %}
> {% block content %}
>   Here you have your main page
> {% endblock %}
>
> # templates/contact.html
> {% extends 'base.html' %}
> {% block content %}
>    <form>
>       Your contact form
>    </form>
> {% endblock %}
> </html>
>
> Then in your app
>
> app = Flask(__nane__)
> ...
> ...
>
> @app.route("/")
> @app.route("/main")
> def main():
>    return render_template("main.html")
>
>
> @app.route("/contact")
> def contact():
>     form = "build your form"
> *    if request.method == 'POST':*
> *        if form.validate() == False:*
> * flash('All fields are required.')*
>    return render_template("contact.html", form=form)
>
>
> If you need to do something different, then you have to render just one
> template and rely only on CSS/Javascript, or maybe deal with that separate
> endpoints via ajax calls.
>
>
>
>
> On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com>wrote:
>
> Hello,
>
> My flask app has single index.html page which has <div class="main">
>  <div class="contact"> etc. in same page but they are shown as different
> pages into the browsers. Its just css trick to show sliders etc. using a
> single page.
>
> Now i have *index.html#main* content page and a *index.html#contact* page
> which has a form through which i want to use this logic.
>
> *if request.method == 'POST':*
> * if form.validate() == False:*
> * flash('All fields are required.')*
> * return render_template('index.html', form = form)*
> *
> *
> But here i want to render_template('index.html#contact') so that it shows
> #contact part of the index.html template.
>
> How can flask/Jinja render <div class="contact"> contact content</div> in
> the index.html page?
>
> Regards
>
>
>
>
>
>
>

Re: 回复: [flask] flask render_template url for

From:
Uday Choudhary
Date:
2013-08-05 @ 04:50
Jus to update my complete logic is:

def index():
form = ContactForm()
if request.method == 'POST':
if form.validate() == False:
flash('All fields are required.')
return redirect(url_for('index')+'#4', form = form)  #form = form doesn't
work here.
else:
msg = Message(form.subject.data, sender='email@mail.com', recipients=['
email@mymail.com'])
      msg.body = """
      From: %s <%s>
      %s
      """ % (form.name.data, form.email.data, form.message.data)
      mail.send(msg)

      return redirect(url_for('index')+'#4', success=True) #sucess=True
need to pass since i would show a success msg   on contact page itself.

elif request.method == 'GET':
return render_template('index.html', form=form)

Its what my complete logic is.


On Mon, Aug 5, 2013 at 10:13 AM, Uday Choudhary <mobulite@gmail.com> wrote:

> Hello Robin,
>
> As you suggested, return redirect(url_for('index')+'#main') works fine.
> But it reloads website.
>
> But i also need to pass some data which so i tried: *return
> redirect(url_for('index')+'#contact', success=True)* but it didn't worked.
>
> I tried *return render_template(url_for('index')+'#contact**')* but i got
> error.* TemplateNotFound: /#contact*
> worked.
>
> Isn't there any way to do something like this : *
> return render_template(url_for('index')+'#contact**') ? *or a way to pass
> data along with #contact trick?
> *
> *
> Regards
>
>
> On Mon, Aug 5, 2013 at 9:50 AM, Robin Deng <gl.deng@gmail.com> wrote:
>
>> ajax is a good way.
>>
>> if you real want "*index.html#main*" ,  try  url_for() :
>>
>> @app.route("/index")
>> def index:
>>     ….
>>     return redirect(url_for('index')+'#main')
>>
>> --
>> Robin Deng
>>
>> 已使用 Sparrow <http://www.sparrowmailapp.com/?sig>
>>
>> 在 2013年8月5日星期一,下午12:06,Bruno Rocha 写道:
>>
>> Not that way. Better is to use a base page and extend it.
>>
>> # templates/base.html
>> <html>
>> <head>......</head>
>> <body>
>> {% block content %}
>> {% endblock %}
>> </body>
>>
>> # templates/main.html
>> {% extends 'base.html' %}
>> {% block content %}
>>   Here you have your main page
>> {% endblock %}
>>
>> # templates/contact.html
>> {% extends 'base.html' %}
>> {% block content %}
>>    <form>
>>       Your contact form
>>    </form>
>> {% endblock %}
>> </html>
>>
>> Then in your app
>>
>> app = Flask(__nane__)
>> ...
>> ...
>>
>> @app.route("/")
>> @app.route("/main")
>> def main():
>>    return render_template("main.html")
>>
>>
>> @app.route("/contact")
>> def contact():
>>     form = "build your form"
>> *    if request.method == 'POST':*
>> *        if form.validate() == False:*
>> * flash('All fields are required.')*
>>    return render_template("contact.html", form=form)
>>
>>
>> If you need to do something different, then you have to render just one
>> template and rely only on CSS/Javascript, or maybe deal with that separate
>> endpoints via ajax calls.
>>
>>
>>
>>
>> On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com>wrote:
>>
>> Hello,
>>
>> My flask app has single index.html page which has <div class="main">
>>  <div class="contact"> etc. in same page but they are shown as different
>> pages into the browsers. Its just css trick to show sliders etc. using a
>> single page.
>>
>> Now i have *index.html#main* content page and a *index.html#contact* page
>> which has a form through which i want to use this logic.
>>
>> *if request.method == 'POST':*
>> * if form.validate() == False:*
>> * flash('All fields are required.')*
>> * return render_template('index.html', form = form)*
>> *
>> *
>> But here i want to render_template('index.html#contact') so that it shows
>> #contact part of the index.html template.
>>
>> How can flask/Jinja render <div class="contact"> contact content</div> in
>> the index.html page?
>>
>> Regards
>>
>>
>>
>>
>

Re: [flask] flask render_template url for index.html#contact

From:
Uday Choudhary
Date:
2013-08-05 @ 04:22
Hello,

Creating different page for contact.html, main.html is not possible.

My template is similar to http://nicinabox.com/superslides/#2 where #1 is
main page, #2 is contact page and so on.

So in such case, i can got for approach you are suggesting.

Please suggest how to render_template() for reaching to mydomain.com/#2 or
mydomain.com/#1 where both #1 and #2 are in same index.html page.

Regards


On Mon, Aug 5, 2013 at 9:36 AM, Bruno Rocha <rochacbruno@gmail.com> wrote:

> Not that way. Better is to use a base page and extend it.
>
> # templates/base.html
> <html>
> <head>......</head>
> <body>
> {% block content %}
> {% endblock %}
> </body>
>
> # templates/main.html
> {% extends 'base.html' %}
> {% block content %}
>   Here you have your main page
> {% endblock %}
>
> # templates/contact.html
> {% extends 'base.html' %}
> {% block content %}
>    <form>
>       Your contact form
>    </form>
> {% endblock %}
> </html>
>
> Then in your app
>
> app = Flask(__nane__)
> ...
> ...
>
> @app.route("/")
> @app.route("/main")
> def main():
>    return render_template("main.html")
>
>
> @app.route("/contact")
> def contact():
>     form = "build your form"
> *    if request.method == 'POST':*
> *        if form.validate() == False:*
> * flash('All fields are required.')*
>    return render_template("contact.html", form=form)
>
>
> If you need to do something different, then you have to render just one
> template and rely only on CSS/Javascript, or maybe deal with that separate
> endpoints via ajax calls.
>
>
>
>
> On Mon, Aug 5, 2013 at 12:49 AM, Uday Choudhary <mobulite@gmail.com>wrote:
>
>> Hello,
>>
>> My flask app has single index.html page which has <div class="main">
>>  <div class="contact"> etc. in same page but they are shown as different
>> pages into the browsers. Its just css trick to show sliders etc. using a
>> single page.
>>
>> Now i have *index.html#main* content page and a *index.html#contact* page
>> which has a form through which i want to use this logic.
>>
>> *if request.method == 'POST':*
>> * if form.validate() == False:*
>> * flash('All fields are required.')*
>> * return render_template('index.html', form = form)*
>> *
>> *
>> But here i want to render_template('index.html#contact') so that it shows
>> #contact part of the index.html template.
>>
>> How can flask/Jinja render <div class="contact"> contact content</div> in
>> the index.html page?
>>
>> Regards
>>
>
>