Some basic questions about Django, Pyjamas and Clean URLs - django

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.

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 + mod_wsgi - How to disable access to files outside of project folder?

I want to make more secure my django virtual host by disabling the access of files that do not belong to my project. So basically I'm looking for similiar like "php_admin_value open_basedir".
Is there a simple way to do this?
Thanks!
There's no need for a setting. As the Django docs state, your code should not be in the docroot anyway:
Where should this code live?
If your background is in plain old PHP (with no use of modern
frameworks), you’re probably used to putting code under the Web
server’s document root (in a place such as /var/www). With Django, you
don’t do that. It’s not a good idea to put any of this Python code
within your Web server’s document root, because it risks the
possibility that people may be able to view your code over the Web.
That’s not good for security.
Put your code in some directory outside of the document root, such as
/home/mycode.
The only thing that should be served is the wsgi file. So the rest of your files are safe.

How to include a small and lightweight standalone application inside a web page in django?

I need to add a currency converter and a calculator to my website that runs on the client side.
something similar to a servlet in java.
Adding a client-side "whatever" to a django project is no different than adding an image, or a video, or anything else. Django doesn't particularly care what you put into your templates. It simply renders the templates server-side to parse your template language code, and then serves up a normal web page to the client.
So the answer to your question is... Do whatever you would normally do in an html page to embed your chosen solution into the page.
The only django-specific issues here could be how to source the path to the media using template tags, which is also no different than sourcing static content like images, javascript, and css. You can read more about that on the django docs: Managing static files

Pyjamas and Django static files setup

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.

Django's admin pages are missing their typical formatting/style, have I set it up wrong?

I finally got my django install working, however I'm noticing that the typical look and feel of the admin pages are missing, and it's as if there are no styles applied to the structure of the pages. Do I have some kind of setup issue or pathing issue that's preventing the style sheets from being found? Where are they stored? My pages look like they are from 1994.
Sounds like your admin media isn't being served correctly. In your settings.py, there's a variable called ADMIN_MEDIA_PREFIX, which specifies the URL where Django should look for them. The actual media files are in "[path to your Python site-packages]/django/contrib/admin/media". When using manage.py runserver, the files are served "automagically". However, when using Apache/nginx/etc it's your responsibility to make sure that your server makes the files available at that URL (using rewrite rules, symlinks, etc). More info can be found here.
I've solved this issue simply with the alias on apache:
Alias /static/admin/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
Alias admin/media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
You need to provide more info for use to help you properly. However, this is most probably because didn't set up your Web server to serve static file, and therefor, the admin CSS is not loaded.
To solve this, got the the admin and look at the HTML source. You'll css the path to the admind css. Make your web server service this file on this path.