I'm building off of the lein new compojure-app template. I've got a route to / which works fine, as well as a route to /foo/ that works. Between the two pages, only the body changes, so I would like to stick as much as I can into a common view function (I'm using the default common in views/layout.clj). However, when serving resources by relative path, like css/main.css, my route to / works fine, but the route to /foo/ is looking for /foo/css/main.css instead of looking in the root directory. How do I serve static resources with relative paths to arbitrary routes?
You need to use wrap-base-url middleware along with include-css. The wrap-base-url will set *base-url* dynamic var which will be used to construct URLs for resources included with include-css, include-js or by using to-url directly.
If you deploy your app as a war in a servlet container, wrap-base-url will autodetect your app root context (e.g. /my-app/). Otherwise you need to provide it the root path manually as the second argument.
Related
I am trying to set an app's config.assets.prefix, to /api/assets. Stylesheet & javascript links work fine, as well as rails image helpers.
However the font-url and image-url sass helpers are still using /assets instead of /api/assets.
The fonts & images are available at both paths, but I need the path to be /api/assets because the app is one of several living behind a Cloudfront distribution, and the behaviour uses the api path prefix to direct requests to the correct origin.
Why are font-url and image-url not respecting config.assets.prefix?
(app is still on Rails 4.2.x)
I have a problem where i have to send two urls of partially same format to different views.
eg. "domain/land/one-brush" will go to views.land(request, id) where id is "one-brush"
and similarly domain/land/one-brush/include/images/dot.jpg will be served statically either by custom view or django static serve.. i prefer static serve.
one thing i have in mind that to write two url patterns one for land// and other for land//anything/will/do. First will redirect to custom url and second one will be served statically..
Any better way would be appreciated.
see how django handles static files using django staticfiles app, note that static files should be put in a totally different directory then other files in your project
STATICFILES_DIRS = (
"/home/special.polls.com/polls/static",
"/home/polls.com/polls/static",
"/opt/webfiles/common",
)
in a real world deployment django should not be serving static files, this work is better done by apache / nginx etc. or even better by some cdn like amazon's / rackspace / google storage etc.
separating static files is usually achieved by using some tool like django pipline that will also help you uglify your files, zip them etc.
anyway, if you still want to serve some urls that start with the same path, bare in mind that django will try to find the first match in the list, meaning you'll want to put:
domain/land/one-brush/include/images/
before:
domain/land/one-brush/
I'd like to serve a Django application from a subdirectory (for example http://www.stackoverflow.com/django_app/).
I've set up mod_wsgi to serve the page via
WSGIScriptAlias /django_app PATH_TO_DJANGO/wsgi.py
How can I specify settings like LOGIN_URL, STATIC_URL, MEDIA_URL, etc. so Django respects the relative path?
If set STATIC_URL it to "/static/" it tries to reference resources at http://www.stackoverflow.com/static/ instead of http://www.stackoverflow.com/django_app/static/.
But if I set it to "static" (without a leading slash) it is interpreted relative to any URL which is also wrong. For example, the admin page at
http://www.stackoverflow.com/django_app/admin/
tries to load the files from
http://www.stackoverflow.com/django_app/admin/static/
I haven't found a way to tell Django to use http://www.stackoverflow.com/django_app/static without explicitly hardcoding the prefix /django_app within the settings (which IMHO violates the DRY principle because it is already specified in the mod_wsgi-config).
It also prohibits serving the same project under different URLs without modifying the project, which seems odd.
There is no way for it to be automatic. The URLs in those few variables, and LOGOUT_URL, are not automatically prefixed with the SCRIPT_NAME which is passed in with scripts and which identifies the mount point.
Is there a straightforward way to set up Django to operate on a directory or set of directories that will also serve other kinds of content?
Right now I have a webserver that is mostly running ColdFusion pages, but I'd like to start moving some sections over to Django. However, due to the existing directory structure it's not ideal to put all of the Django stuff in just one web path. Ideally I'd like to be able to keep using the original directory structure rather than having to use a lot of redirects.
Is there any way to make Django play nice with other things, or does it pretty much need its own root to be happy? The only other solution I can think of is carefully configuring the web server with a lot of rules that purposefully sidestep Django when necessary (for example, instructing it to manually handle anything with a file extension, or to ignore certain directories).
This would be on IIS, if it happened.
You can configure URLs in Django anyway you like. Have a look at the URL dispatcher. So say for instance your site www.example.com, you decide to have /wiki and /blog be developed using Django. You can configure IIS to redirect those urls to Django, while the rest of www.example.com/everythingelse is served by Coldfusion or whatever.
Even a mixed url scheme say /store/mycoldfusion-product-view and /store/django-product-view would be possible though this would require some amount of fancy redirect code depending on your setup.
It sounds as if you have some control over what is being served when. If that's the case, could you use a reverse proxy to segment your namespace? I do this with a lot of different projects; I use nginx, and tell it "these paths are for Wordpress, these paths are for Django, and these paths are for images and other static content."
That's an excellent way of making Django "play nice," as long as you have a disciplined approach to converting some of the paths to one or the other.
An alternative way to set this up would be to daisy chain:
Webserver -> django -> response middleware -> subprocess/pipe/httplib -> coldfusion.
The response middleware would pseudo code something like this:
if response.code in [list of ok responses]:
return
else:
call coldfusion
The advantage of this method would be that you can transition at whatever rate you want. The disadvantage is that it isn't a simple configuration, daisy chains are brittle by nature, and the daisy chain might break.
Let's say you've setup your site using Pylons, Django and most of the site runs fine and according to the framework used. However, what if you had a custom section that was entirely say, composed of flat html files and its own set of images, which you didn't have time to actually incorporate using the framework and were forced to basically support, under the same domain? Should there be some sort of default controller/view that's super bare minimalistic or do frameworks such as these somehow offer support in some smart way?
I realize also that potentially one could setup a new subdomain and reroute it to an entirely different directory, but I'm just curious as to how one would solve this when forced to deal with a framework.
When serving static pages I'd rather avoid having Django or Pylons handle the request, and handle it with the web server only. Using Nginx, you'd use a directive like:
location / {
root /whatever/the/path/is/;
# if the file exists, return it immediately
if (-f $request_filename) {
break;
}
# pass requests to MVC framework
# i.e. proxy to another server on localhost:
proxy_pass http://127.0.0.1:80;
}
For pylons you should be able to drop your static html files in the public directory. If there isn't a controller for a url then I think pylons looks in the public folder next.
For Django, I would serve these in exactly the same way as you serve your static assets - in your site_media directory, along with subdirs for js, css and img, you could have an html directory. Then the URL would just be /site_media/html/whatever.html.
In Django take a look at flatpages. It's part of the django.contrib package and uses flatpages middleware to serve up flat HTML controlled through the admin interface. For basic purposes, serving up additional about pages or the like this should do the trick.
You could also just create an HTML folder and - using mod_python, at least - set no handler for that path in the Apache configuration file (e.g. vhost.conf).