Pyjamas and Django static files setup - django

I am somewhat new to both web development and new to the Django and Pyjamas frameworks. So I appreciate any patience offered to me as I learn.
I am setting up Django and Pyjamas to work together, JSONRPC Client/Server + Django templates.
I have the compiled Pyjamas my_project.html being served as Django templates. I have the Django template folder set to my Pyjamas project output folder.
Pyjamas compiles a bootstrap.js to the same output folder and I need proper referencing within the Django templates to these files as the current reference of:
<script language="javascript" src="bootstrap.js"></script>
Is not functioning.
The Django way of managing static files is not very obvious to me even after reading it's documentation. What is the best way to set this up in terms of folder hierarchy and Django settings?
What is the best way to reference my static files from within my Django templates?
I know I am likely asking obvious questions but after reading the available documentation I just can't seem to put this together. This is the last piece of the puzzle before I really start rolling so any help will be appreciated greatly!

It won't let me comment but I am running into the same issue. To answer Daniel Kluev, our reason for serving the pyjamas page through django is because JSONRPC requests from pyjamas -> django run into the CSRF protections in django, resulting in a 403 error. The best solution (and maybe only?) that we have seen is to pass the token to pyjamas as a cookie (or form element), which can then be sent back to django during the RPCs. We have not been able to implement this due to the issues Shattered1113 mentioned in his question.
The only other option I've seen is to turn off the CSRF middleware, or exempt specific views from it using a decorator. This seems to be the solution everyone uses, however we require the csrf protection to be enabled so it will not work for us.

Related

Django - Best way to organize/serve mostly static website?

My friend with very little coding experience began making a business site and passed it on to me. Currently, it is mostly static html and css files but I want to take what he has and build on it using Django because I will be adding features like user login/auth and checkout. I was wondering what the best thing to do with all these html and css files are and how I might go about fitting it to Django framework.
All of the content on these pages will be static but I could imagine in the future once I add a login, the header might be different to show whether or not a user is logged in. Would I need to make separate apps for each page, include them all in one app, or just put them all in the static folder?
Would I need to make separate apps for each page
Assuming you are referring to django apps. Then No, you do not need to create a separate application for each page. django apps are a way to organize individual pieces of your projects.
To gain the most out of django, I would suggest looking into the Django Template Engine to improve the html (blocks, include and extend etc) and make it more readable and future proof ( urls, media, forms)
just put them all in the static folder?
I'm not sure you'd be able to get away with it being in the static folder, Static files in django are used for CSS and Media files such as images, videos etc. To render a template in django you need use render() or TemplateView
I hope this helps clear up some of your doubts.
Django is a Model-View-Template (MVT) framework where you create templates (HTML files) and use Jinja syntax there to display the data passed from Django views. You don't need to create apps for each page, instead, you can define functions in an app's views.py file and then reference that view from the corresponding path (URL) from the urls.py file.
You can create a main HTML template that will be extended by each sub-page. And applying your logic to your main template's header (i.e. if/else) in the main template will affect your all pages.
My recommendation is that you should first study the Django docs carefully and understand at least it's basics before starting the migration of the HTML site to Django. Django has one of the best documentation available for software on the Internet and you should be grasping the basics very quickly if you have a little programming background.

django collectstatic with template processor

Is it possible to use templates for static files? I would like django could process the static files with template processor before copying them in the static folder when manage collectstatic is issued.
Of course the context would not contain request information but it could be useful to use such information as:
urls from url names to be used in javascript code
settings to be used to customize css or static html pages
What is the simpler way to accomplish this?
At least in the case of JavaScript I have previously done it. Please see the reference for calling a JavaScript service worker in the following link:
Django and service workers - serve "sw.js" at application's root url
As far as the knowledge if it is a good design architecture I would not know. But may seem and interestingly enough idea to give it a try.

django admin django.contrib.staticfiles

I'm following the tutorial contained here:
http://www.djangobook.com/en/2.0/chapter06.html
They say that the admin site should look like this:
http://www.djangobook.com/en/2.0/_images/admin_index.png
When I start the admin site, though, it looks really simplistic, just plain text and links:
Django administration
Welcome, admin. Change password / Log out
Site administration
Auth
Groups Add Change
Users Add Change
Recent Actions
My Actions
None available
I noticed that it looks all nice like the link when I uncomment django.contrib.staticfiles from the INSTALLED_APPS, although that wasn't mentioned in the tutorial...can someone please explain this behavior to me?
Thank you for your help!
The Django Book is a little out of date (although an update is in the works I believe):
This book was originally published by Apress in 2009, and covered Django 1.0. Since then, it’s languished. We’re working on getting the book updated to cover Django 1.4, 1.5, and beyond
Static files are all the CSS/JS & images that your site (and the django admin) uses. They need to be collected and placed somewhere that your server (or development server) can serve them. This is the job of django.contib.staticfiles.
You can read more about this in the 'Managing Static Files' documentation
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them.

Django URL conf and Backbone.js Router

I have a backbone.js single-page app that is all set up with the router (well, actually a Backbone.Marionette app with a Backbone.Marionette AppRouter, but nevertheless). However, the backend is based in Django, where I do not have the URL conf directing to views for all URLs that are already in the backbone.js routes.
Based on the existing URLs in the Django URL conf, Backbone.js will serve the backbone routes regardless of what is listed in the Django conf - it seems something, anything just needs to be there.
Do I need to have proper Django views in order to offer a fallback for older browsers/SEO?
What are the best practices to coordinate the Django URL conf and the Backbone.js Router?
I've found a post that addresses this issue quite well:
http://duganchen.ca/single-page-web-app-architecture-done-right/
Briefly, my reasoning for including a fallback is for non-javascript browsers and SEO reasons. At the time of this post, non-javascript browsers account for ~1.4% (less than 2% from everything I've read) of users, making SEO The major consideration. Again, SEO may not be relevant for everyone reading this post, in which case, this can be skipped.
I found Thomas Davis' tutorial using phantom.js quite helpful. http://backbonetutorials.com/seo-for-single-page-apps/
However, another issue that I needed to account for was the history API, which has been neglected by all but the latest IE browsers. Given my client's users, about 15% of which are using IE <= 9, this was also a problem.
In the end, I also needed to use history.js. All in all, this was a lot of work to update an otherwise very simple website. However, I learned a lot from this ordeal.
In my opinion if your backbone app is truly a single page then you don't need any django views whatsoever. You can serve your index.html as a static file (in production, not even by django) and then let backbone's router take care of your url configuration, as you're doing already. You can use backbone's history and navigate to fake urls, add urls parameters etc, for resources in your app.

Some basic questions about Django, Pyjamas and Clean URLs

I am farily new to the topic, but I am trying to combine both Django and Pyjamas. What would be the smart way to combine the two? I am not asking about communication, but rather about the logical part.
Should I just put all the Pyjamas generated JS in the base of the domain, say http://www.mysite.com/something and setup Django on a subdirectory, or even subdomain, so all the JSON calls will go for http://something.mysite.com/something ?
As far as I understand now in such combination theres not much point to create views in Django?
Is there some solution for clean urls in Pyjamas, or that should be solved on some other level? How? Is it a standard way to pass some arguments as GET parameteres in a clean url while calling a Pyjamas generated JS?
You should take a look at the good Django With Pyjamas Howto.
I've managed to get the following to work, but it's not ideal. Full disclosure: I haven't figured out how to use the django's template system to get stuff into the pyjamas UI elements, and I have not confirmed that this setup works with django's authentication system. The only thing I've confirmed is that this gets the pyjamas-generated page to show up. Here's what I did.
Put the main .html file generated by pyjamas in django's "templates" directory and serve it from your project the way you'd serve any other template.
Put everything else in django's "static" files directory.
Make the following changes to the main .html file generated by pyjamas: in the head section find the meta element with name="pygwt:module" and change the content="..." attribute to content="/static/..." where "/static/" is the static page URL path you've configured in django; in the body section find the script element with src="bootstrap.js" and replace the attribute with src="/static/bootstrap.js".
You need to make these edits manually each time you regenerate the files with pyjamas. There appears to be no way to tell pyjamas to use a specific URL prefix when generating together its output. Oh well, pyjamas' coolness makes up for a lot.
acid, I'm not sure this is as much an answer as you would hope but I've been looking for the same answers as you have.
As far as I can see the most practical way to do it is with an Apache server serving Pyjamas output and Django being used as simply a service API for JSONrpc calls and such.
On a side note I am starting to wonder if Django is even the best option for this considering using it simply for this feature is not utilizing most of it's functionality.
The issue so far as I have found with using Django to serve Pyjamas output as Django Views/Templates is that Pyjamas loads as such
Main html page loads "bootstrap.js" and depending on the browser used bootstrap.js will load the appropriate app page. Even if you appropriately setup the static file links using the Django templating language to reference and load "bootstrap.js", I can't seem to do the same for bootstrap.js referencing each individual app page.
This leaves me sad since I do so love the "cruftless URLS" feature of Django.