Frozen flask creates the files but gets the links wrong - python

Using frozen flask to make my website static, I have the following problem.
While all of my pages are being built (file//c:/correctpath/build/2014/page-title/index.html) the links to the pages are file:///c:/2014/page-title/.
Is there something I have missed?
EDIT:
In my template I have something like
{% for page in pages %}
{{ page.title }}
{% endfor %}
where .url() is a method on the page object:
return url_for('article', name=self.name, **kwargs)

url_for produces absolute paths (e. g. /2014/page-title) - when you open up your files in the browser it follows the rules regarding relative URL resolution and strips the extra file contents. If you just want to view your files as they will be seen on the server, Flask-Frozen has a run method that will let you preview your site after generating it.
Alternately, you can set FREEZER_RELATIVE_URLS to True to have Flask-Frozen generate links with index.html in them explicitly.

Rather than setting FREEZER_RELATIVE_URLS = True, with resulting URLs ending on index.html, you can also set FREEZER_BASE_URL to <http://your.website/subdir>.

Related

Get information from python file in html [web app using flask]

So I am making a web app in Flask. In the layout.html file, there is a navbar on the top of the screen. It has 2 elements: Create Family and Join Family. I only want these to appear when a user a not created or joined a family.
I cannot even use the parameters of render_template() becuase the layout.html is not rendered anywhere in my app.py file
Normally, you'd have in your app.py a route that returns the template:
return render_template('child.html', data=data)
child.html extends layout.html.
data contains everything your template needs.
In your template, you'll have an if
{% if data.show_create_join_family %}

Is there any way to implement list of different links in Flask application?

I am trying to create a blog page where my requirement is to list a group od links in a html page.
app.py:
#app.route("/blog")
def blog():
return render_template('blog.html')
blog.html:
{% extends "index.html" %}
{% block content %}
<ul>
<li>
Chapter 1
</li>
<li>
Chapter 2
</li>
<ul>
{% endblock content %}
Stored all my html pages in templates directory.
I am able to open the localhost:8000/blog where I can view all the Chapter1, chapter2 links, but when I click on the link I get error:
http://localhost:5000/templates/chapter%201.html
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
I am sure that this will not work as it is pointing to different URL, can some one guide how do I implement the list of Chapter in Flask app?
If you had the static html files already, you should serve it as static file as mentioned in document here. Then, put urls for these files in your main page:
url_for('static', filename='chapter1.html')
The templates directory is not linked to a URL, the files in that directory are not 'published'. You shouldn't put files there you intent to serve without processing (executing the templates).
With the default configuration only files in the static directory are accessible via the /static/ URL path prefix, see the Static Files section of the Flask Quickstart.
The intent for the templates directory is for the files to be loadable as Jinja2 templates using the render_template() function, where you use that function in endpoint functions you registered with Flask for specific URLs.
You may want to try out the Flask tutorial first, it covers both templates and static files in more detail.
For a blog site, the normal pattern would be for you to store the blog post data in a database or text files in some way Python can easily load, then use a single template to render the contents. You'd register a URL with a pattern, which would call a function that can load any blog page data, then use the blog page template to render the output:
#app.route('/blog/<page_name>')
def blog_page(page_name):
page_data = load_blog_page(page_name)
if not page_data: # no page? Produce a not found error.
abort(404)
return render_template('blog_page.html', **page_data)
The above function will be called when a browser visits URLs that start with /blog/, and is called with the next part as the page_name variable; e.g. /blog/chapter-1 would result in page_name being set to 'chapter-1'. Note that URLs can't have spaces in them (spaces would have to be encoded to %20 for the URL to still be a URL).
The function then loads the page data (with a load_log_page() function you'd have to write yourself), then provided page_data is not empty or None or some other false-y value indicating that the page doesn't actually exist, uses the render_template() function to produce HTML output. render_template() will use the file templates/blog_page.html as the template file here, and page_data is assumed to be a dictionary whose key-value pairs make the variables the template can use.

how to use dynamic templates in python pyramid

i've finished developing a website, it's working fine however i am trying to optimize my website by adding dynamic templates, and want to make sure that if it can be done on pyramid python.
for example, in my jinja template i have the following:
{% block article_detail %}
<form action="{{request.route_url('Sports_News_Action',action=action)}}" method="post" class="form">
{% if action =='edit' %}
{{ form.id() }}
example in my controller:
#view_config(route_name='Sports_News_Action', match_param='action=create',
renderer='StarAdmin:templates/edit_sports.jinja2')
def general_create(request):
entry = SportNews()
the request route will have to match the one in my controller in order to run the function. what i want to do is how do i replace the one in jinja with a dynamic variable, to use the one jinja template lets say for different views/controllers with different route_names.
I think in your situation the simplest solution is to leave action undefined and the browser will submit the request to the current url. You only need to specify action if you want to submit the form to a different url than the current. That being said, you can use lots of different options in pyramid to generate a url as well. For example, request.url is the current url, or request.matched_route.name is the name of the current matched route.

Django 1.10 - get site base url

How do you get base url of a site?
like - https://stackoverflow.com/
I would love to set it to settings.py
Thanks
Answer:
Really very sorry for such a poor question. :(
I was trying to link up a post by following code -
{{ related_question.question_text }}
it took me from current page url but i expected from base url. I thought, i would get a solution like site_url() like Wordpress and could use it in settings.py.
But i don't have to do that. Starting href with /.... starts from base url. So, above <a> tag should be -
{{ related_question.question_text }}
That's it.
I recommend you to write your own template context processor which will return a SITE_URL from your settings file.
See https://docs.djangoproject.com/en/1.10/ref/templates/api/#writing-your-own-context-processors
Your function simply has to return a dict such as {"SITE_URL": settings.SITE_URL} and then all your templates will have "SITE_URL" in the context.
Another option would be to add a method to the question model e.g. get_url() which returns a site url with a slug and then use related_question.get_url inside your template.
And you should probably use a "url" tag in your templates, such as {% url 'question' related_question.id %} instead of hardcoding the URLs.

Is there feature in Pyramid to specify a route in the template like Django templates?

For example in Django if I have a url named 'home' then I can put {% url home %} in the template and it will navigate to that url. I couldn't find anything specific in the Pyramid docs so I am looking to tou Stack Overflow.
Thanks
The brackets depend on the templating engine you are using, but request.route_url('home') is the Python code you need inside.
For example, in your desired template file:
jinja2--> {{ request.route_url('home') }}
mako/chameleon--> ${ request.route_url('home') }
If your route definition includes pattern matching, such as config.add_route('sometestpage', '/test/{pagename}'), then you would do request.route_url('sometestpage', pagename='myfavoritepage')

Categories