Can you serve static HTML pages from Pyramid? - python

I have a Pyramid app using Mako templates and am wondering if it is possible to serve static HTML pages within the app?
For the project I'm working on, we want to have relatively static pages for the public "front-facing" bits, and then the application will dynamically serve the meat of the site. We would like one of our internal users to be able to edit some of the HTML content for these pages to update them.
I have my static folder that I'm serving CSS and scripts from, but that doesn't seem to really fit what I'd like to do. I could create views for the pages and basically have static content in the mako templates themselves but I think the application would need to be restarted if someone were to update the template for the changes to appear? Maybe that's not the case?
Long term I would probably do something like store the content in a db and have it dynamically served but that's outside of the scope at this time.
Is there a reasonable way to accomplish this or should I not even bother and set up the public pages as just a regular static HTML site and just link to my app altogether?
Thanks!

You can serve static html from Pyramid by using views that return pre-fabricated responses. You'll have a more fun time doing it though by just having your web server serve static html if it finds it, otherwise proxying the request to your Pyramid app.

Related

How to insert create-react-app project into existing python/flask app

We have an existing python/flask app (made by someone else) that has a nav bar, sidebar and different pages. We have built a create-react-app project that we want to sit on one of these pages in the python/flask app.
The question is - how can we integrate our react app into the existing python/flask codebase?
Any help gratefully received!
Update: from helpful answer below - could anyone help with explaining this a little more - perhaps with code examples? "render a container in the same template that react can mount on."
Update 2: Someone else suggested "You can have React run anywhere where you can add a script tag into HTML and a DOM element to attach it to. As long as you have a way to bundle and serve the JS files, you should just be able to include your new app like that." - could anybody help with code examples of this?
A rough outline would be:
configure the build folder to be the folder where static files are served from by your server (js, css, ...)
include the files from that folder in the template of the page your want your react app to display (via <link> and <script> tags).
render a container in the same template that react can mount on.

How to right code in Django and AngularJS?

Now I am working with Django on server side and jQuery on client. Django views functions return templates with js code.
How it should look using AngularJS. Should I return JSON from Django and render response using JS ?
Many thanks!
It really depends on what you want to do, there are many ways, but yeah, ideally your routes and templates are handled in AngularJS, Angular requests information from Django or POSTs information, and they communicate using JSON.
You need to put your Angular templates and files in the static folder, you can use grunt or django-pipeline to better manage all the files you have.

Managing multiple apps with pyramid

I recently moved from Django to Pyramid because my traffic increased and I needed a better scalable framework.
My django project was structured in this way:
user(app): views, models, forms, templates, etc
download (app): views, models, forms, templates, etc
store (app): views, models, forms, templates, etc
static: all the static files (images, css, etc..) in the same folder because they are shared between the applications
Now I'm trying to replicate the same project structure with Pyramid, but every application has its own static-files folder.
I need to create a common static folder.
How can I achieve that?
You can use config.include() method to emulate your Django applications.
Shared static folder can be easily done using static views, that can point to any folder in the file system. See Serving Static Assets in the documentation.

How to serve django templates to javascript

I am using django on the server side and obviously javascript on the client side. Now i want to use the plate template engine on the client.
What's the best way so serve django templates to the client? We taught of some ways doing that.
Create a view that serves the raw templates.
probably not the best method
Copy the needed templates to the static folder.
this could be done with a custom static files finder
the broser is able to cache the templates
Provide the templates using a template tag which puts the raw template into a javascript variables.
templates received this way can not be cached seperatly
is a django app out there that makes this easyer?
The reason i need the templates on the client is, that i want to use the same clients on the server and the client side. When the page is first loaded, the full template is rendered on the server, when navigating trough the application only the needed data gets loaded and the page change is done using push state.
If you need to be able to have A) dynamically generated plate templates, or B) dynamically created plate templates (e.g., entered into the DB via the admin, etc.), You'll want to go with 1 (not a bad thing - django is made for serving text content, so as long as you need to have it in a dynamic manner, there's no problem doing it). 3 is a bad choice, because it means that a browser can't cache the static resource (if it's output into each page)... unless you need different plate templates for each page of course.
If you don't need A or B from above, I'd just stick the templates in your static dir, as you mentioned (e.g., collectstatic or simply add them to your repo, if they're a part of your app).
Regarding an app that makes this easy - you could look at Django Chunks (output a static chunk into a place in the page, like `{% chunk "header-snippet" %}), but I don't think you need that.

What is the best way to serve static web pages from within a Django application?

I am building a relatively simple Django application and apart from the main page where most of the dynamic parts of the application are, there are a few pages that I will need that will not be dynamic at all (About, FAQ, etc.). What is the best way to integrate these into Django, idealing still using the Django template engine? Should I just create a template for each and then have a view that simply renders that template?
Have you looked at flat pages in Django? It probably does everything you're looking for.
If you want to just create a template for each of them, you could use the direct_to_template generic view to serve it up.
Another option would be the django.contrib.flatpages app, which would let you configure the static URLs and content via the database.

Categories

Resources