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.
Related
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.
I am trying to create my personal web page. So in that I needed to put in the recommendations panel , which contains recommendations by ex employees/friends etc.
So I was planning to create a model in django with following attributes:-
author_name
author_designation
author_image
author_comments
I have following questions in my mind related to image part:-
Is it good practice to store images in the backend database?(database is for structured information from what i understand)
How to store images so that scaling the content and managing it becomes really easy?
in short: no.
use Django's built in ImageField and have your webserver serve the files from disk.
Alternatively you can use ImageField with a custom storage backend such as django-storages and put the files up on e.g. Amazon S3 and have them served from there (maybe adding something like CloudFront CDN in front)
No.Not good, especially as it scales.
https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/#serving-static-files-in-production
When you think about what happens in the request/response cycle you'll know that your python scripts get interpreted by some modules. So if you're using apache for instance, mod_wsgi could be doing this work.
Usually, you don't want your static files being served by this same process because that is not very efficient, static files being static. In a typical scenario, you'll want a very fast web server, say nginx serving your static content without "thinking". This delegation gives a very efficient and scalable design. As #Anentropic said, you could choose to host static media on a CDN.
The best way to do this is to store the images in your server in some specific, general folder for this images. After that you store a string in your DB with the path to the image that you want to load. This will be a more efficient way to do this.
I have a template that I would like to cache for django. The url is something like this:
/cache_my_page/<object_number>/
There are about a million objects here -- so 1M pages I would like to cache. Is there a way to pre-cache all these pages, before a user loads it? Or does django not offer that, and I need to dive into something like redis or memcache to do that?
You can compile your templates outside a request-response, but that's probably not what you want. Django is for dynamic content. Though if your objects hardly ever changes you could certainly loop over your objects, dump the resulting content of the request into a static file and let it be served by nginx.
https://docs.djangoproject.com/en/1.7/ref/templates/api/#compiling-a-string
Though there are more elegant solutions for dynamic content as well.
How you can avoid rerendering a url which was requested before, is described here in detail:
http://www.djangobook.com/en/2.0/chapter15.html
Or have a look at a solution like Varnish taking Django out of the equation:
Varnish is a piece of software that sits between our load balancers
and our Django backends and acts as an HTTP caching layer. What this
means is that it can cache the entire HTTP response without even
hitting a Django server, if we know that request won’t be unique.
http://blog.disqus.com/post/62187806135/scaling-django-to-8-billion-page-views
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.
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.