serve static files css and js via wheezy.template? - python

I have this master html template:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Start Bootstrap - SB Admin Version 2.0 Demo</title>
<!-- Core CSS - Include with every page -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.css" rel="stylesheet">
<!-- SB Admin CSS - Include with every page -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Core Scripts - Include with every page -->
<script src="js/jquery-1.10.2.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<!-- SB Admin Scripts - Include with every page -->
<script src="js/sb-admin.js"></script>
</head>
this is the test.py file:
from wheezy.template.engine import Engine
from wheezy.template.ext.core import CoreExtension
from wheezy.template.loader import FileLoader
T = ['where/project/folderbase/is']
engine = Engine(
loader=FileLoader(T),
extensions=[CoreExtension()]
)
master_template = engine.get_template(r'master.htm')
#route('/test')
def login_name():
return master_template.render({})
I'm a complete n00b at templating and web design.
lets say i run this via any of python web server like flask on localhost:port/test
Nothing shows up.
Why?
And what is this #path_for in wheezy.template?
Do I need to include #require(path_for) or anything else?
is that necessary to server static files in the html file to define all the files in a specific folder-> 'static'
or can they be accessed from where they are now, as in the code above?

You had lots of questions. I'll answer even though you may not care anymore...
If you have correctly configured Flask, and served that template on the route/url 'test', then nothing would appear as you have not defined a <body> with any content in the html.
In wheezy.templates, you access local variables/functions with the #my_variable syntax (ie you prefix it with an # symbol). If you want to access a variable that was passed to the template as part of the context, you need to require it first, #require(my_variable). Your example uses an empty dict as the context, so there would be no variables to access/require.
path_for is part of wheezy.routing, not wheezy.templates. It is used for getting the url of a named route (ie you could do #path_for('test'), and it would return localhost:1234/test. Using path_for would only make sense if you are using the complete wheezy.web framework (which uses wheezy.routing and wheezy.templates). Flask would have its own functions for doing this (I'm not sure what they are though, I don't use Flask). You would need to pass these functions into the template via the context, then #require them to use them though (or make some custom extension for wheezy.template).

Related

How to run compiled vue project in django

Previously, I know how to run Vue and Django (jinja2 template) together.
by handling the custom delimiters, e.g delimiters: ['[[', ']]'].
But for some reason, my supervisor just need to run the compiled vue project inside my existing django project. As we can see, the vue has npm run serve or yarn run serve to run it.
Does django can handle this case? If yes, what I should do?
In this case, we doesn't use direct web server like nginx, apache, etc to run.
Charanjit Singh answer is correct and your 404 problem is not related to vueJs. Since you are not using a direct web server you are making it harder.
Also If your vue application implements vue-router in history mode that'll cause even more problems since you're not using neither nginx or apache.
My only approach for this is Haproxy that'll make your sub application handle those routes.
For example your app domain is myawesomedomain.com and your vue app is in myawesomedomain.com/myvueapp then you need to configure your Haproxy to let your vueapp handle all routes in myawesomedomain.com/myvueapp/*.
If you don't have a vue-router in your app then you need to place th vueapp folder in your deployed web folder and don't forget to add a routing rule for your html file (I don't know about Django but I did it with symfony and it is working)
Deployed
|
|_vueapp ===> your compiled folder
|
|_htmlFile ===> your html file
Finally, this is what I've been working on. I put the compiled index.html file from dist/index.html to templates/vueapp/index.html (django templates folder). The other files & folders is put inside static/ folder.
templates/vueapp/index.html => the compiled html file.
static/vueapp/ (includes: css, js, fonts, etc). => the compiled vue static files.
my views.py;
from django.views.generic import TemplateView
class VueAppView(TemplateView):
template_name = 'vueapp/index.html'
and my urls.py;
from django.contrib import admin
from django.urls import (path, include)
from my_app.views import (HomeTemplateView, VueAppView)
urlpatterns = [
path('admin/', admin.site.urls),
path('', HomeTemplateView.as_view()),
path('vueapp/', VueAppView.as_view()),
]
also inside the vueapp/index.html. As we can see, I modified the source of /static/vueapp/ and linked to the static folder.
<!DOCTYPE html>
<html lang=id>
<head>
<meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1">
<link rel=icon href=/static/vueapp/favicon.ico>
<title>siap-ums</title>
<link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900">
<link rel=stylesheet href="https://fonts.googleapis.com/css?family=Material+Icons">
<link href=/static/vueapp/css/chunk-01c619b4.ff54e24d.css rel=prefetch>
<link href=/static/vueapp/css/chunk-308637dc.e4c5f763.css rel=prefetch>
<link href=/static/vueapp/css/chunk-616b136f.404f3685.css rel=prefetch>
<link href=/static/vueapp/js/app.876efdb8.js rel=preload as=script>
<link href=/static/vueapp/js/chunk-vendors.2b11f5ad.js rel=preload as=script>
<link href=/static/vueapp/css/chunk-vendors.a6a7bf01.css rel=stylesheet>
</head>
<body>
<noscript><strong>We're sorry but siap-ums doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript>
<div id=app></div>
<script src=/static/vueapp/js/chunk-vendors.2b11f5ad.js></script><script src=/static/vueapp/js/app.876efdb8.js></script>
</body>
</html>
Every times the vueapp have changes, I must compile it first and do the same step as above.

Flask static file content type is text/html after redirect

I have run into a problem with static files after a redirect in Flask.
The web app I am writing will redirect users after logging in for the first time to a site where they need to change the default password they've been provided. On this site my custom stylesheets are entirely broken and images won't display either.
A quick inspection in the Chrome dev tools showed that after the redirect the content type for the all files from the static folder is set to text/html. I suspect that this is the issue.
In my template html the stylesheet is linked in the following way:
<link rel="stylesheet" href="{{ url_for('static', filename='css/colorVariables.css') }}">
The stylesheets and images work fine on every other page of the app and also if you go to the exact same page without the redirect.
The redirecting part looks like this:
if user.first_login:
return redirect(url_for("change_password"))
I have found the issue. The redirect was happening in a function run with app.before_request. Wrapping the routes with the function manually solved the problem.

Generate pages with Flask without using separate template files

I'm trying to keep things minimal, so I don't want to create templates, directory structures, etc. I have a simple CSS file available on the web (via RawGit) and I want to use it to style the page generated by a view. How can I render a page without templates?
from flask import Flask
application = Flask(__name__)
# <insert magic here>
# application.get_and_use_my_super_cool_CSS_file(URL)
# <insert magic here>
#application.route("/")
def hello():
return "hello world"
if __name__ == "__main__":
application.run(host = "0.0.0.0")
If you don't want to write templates as separate files, you can use render_template_string instead. This is typically not a very useful feature, as it becomes difficult to maintain large templates written as Python strings. Flask's Jinja env also provides some extra help when rendering files it recognizes, such as turning on autoescape for HTML files, so you need to be more careful when rendering from strings directly.
return render_template_string('''<!doctype html>
<html>
<head>
<link rel="stylesheet" href="css url"/>
</head>
<body>
<p>Hello, World!</p>
</body>
</html>
'''

Why does Django template script src only works if hard-coded?

In order to avoid mixing non-SSL and SSL content, I want to use a relative path for my Javascript file. In settings.py, I have defined JS_ROOT = "/path/js/".
In my html file, the following line results in no mixed content warning:
<script src="/path/js/Foo.js" type="text/javascript"></script>
This line gives a mixed content warning:
<script src="{{ JS_ROOT }}Foo.js" type="text/javascript"></script>
Any explanation?
I discovered that my problem was that I was not returning JS_ROOT correctly as part of my request context. Now that I include it in my context_processors.py and have the context_instance as part of my response, it works.

static_url calling in Tornado

I am a beginner to Python and Tornado web framework. When I was studying the template part in the book "Introduction to Tornado", one line confused me:
<link rel="stylesheet" href="{{ static_url("style.css") }}">
How can the application know where to call the function static_url when there is no library imported to the namespace? I found static_url() in Tornado's web module, but I can not figure out how can this function be successfully called in that template file?
The RequestHandler class has a method get_template_namespace(), documented here. If you click the source link then you'll see how it creates a dictionary where 'static_url' is set to self.static_url.

Categories

Resources