How to serve staticfiles with full URL for local development?

989 views
Skip to first unread message

e.generalov

unread,
May 5, 2012, 9:34:47 AM5/5/12
to Django users
There was a snippet to display a content of response in browser during
debugging http://miniblog.glezos.com/post/3388080372/tests-browser .

"One of the first issues you might face is seeing a style-less page.
This happens becuase the test server isn’t really a web server, and
you’re probably serving static files from something relative such as
'/
site_media'. The solution is simple: Run a separate Django server and
tweak your development-only static URL to something like:

STATIC_URL = 'http://localhost:8000/site_media/
"

but this doesn't works anymore, because django.contrib.staticfiles
doesn't serve static when STATIC_URL contains full URL.

I found the node at https://docs.djangoproject.com/en/dev/howto/static-files/#serving-sta...
:

"That's because this view is grossly inefficient and probably
insecure. This is only intended for local development, and should
never be used in production.

Additionally, when using staticfiles_urlpatterns your STATIC_URL
setting can't be empty or a full URL, such as http://static.example.com/."

Is there a way to omit this limitation for local development?


(reposted from
http://groups.google.com/group/django-developers/browse_thread/thread/2dcaab0939455308/c422ab645b335513#c422ab645b335513)

Reinout van Rees

unread,
May 5, 2012, 6:41:54 PM5/5/12
to django...@googlegroups.com
On 05-05-12 15:34, e.generalov wrote:
> There was a snippet to display a content of response in browser during
> debugginghttp://miniblog.glezos.com/post/3388080372/tests-browser .
>
> "One of the first issues you might face is seeing a style-less page.
> This happens becuase the test server isn�t really a web server, and
> you�re probably serving static files from something relative such as
> '/
> site_media'. The solution is simple: Run a separate Django server and
> tweak your development-only static URL to something like:
>
> STATIC_URL = 'http://localhost:8000/site_media/
> "
>
> but this doesn't works anymore, because django.contrib.staticfiles
> doesn't serve static when STATIC_URL contains full URL.

I don't have any problems with static stuff. The solution is probably
simply to not include any hostname. What I have (on the server and
locally on my development box):

STATIC_URL = '/static_media/'


In case it still doesn't work: you probably use something else than the
standard runserver. In that case,
https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#django.contrib.staticfiles.templatetags.staticfiles.django.contrib.staticfiles.urls.staticfiles_urlpatterns
applies. You have to add that to your urls.py. But normally, you don't
have to.



Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

e.generalov

unread,
May 10, 2012, 3:25:31 PM5/10/12
to Django users


On 6 май, 04:41, Reinout van Rees <rein...@vanrees.org> wrote:
> On 05-05-12 15:34, e.generalov wrote:
>
> > There was a snippet to display a content of response in browser during
> > debugginghttp://miniblog.glezos.com/post/3388080372/tests-browser .
>
> > "One of the first issues you might face is seeing a style-less page.
> > This happens becuase the test server isn t really a web server, and
> > you re probably serving static files from something relative such as
> > '/
> > site_media'. The solution is simple: Run a separate Django server and
> > tweak your development-only static URL to something like:
>
> > STATIC_URL = 'http://localhost:8000/site_media/
> > "
>
> > but this doesn't works anymore, because django.contrib.staticfiles
> > doesn't serve static when STATIC_URL contains full URL.
>
> I don't have any problems with static stuff. The solution is probably
> simply to not include any hostname. What I have (on the server and
> locally on my development box):
>
> STATIC_URL = '/static_media/'

In order for static files at the temporary saved page,
become accessible to the browser,
them urls must be a full.

-- /tmp/pageXXXXXXX.html --

<img src="http://localhost:8080/static/img.png">

(this suggestion has discribed at the sinppet's page).

>
> In case it still doesn't work: you probably use something else than the
> standard runserver. In that case,https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#django...
> applies. You have to add that to your urls.py. But normally, you don't
> have to.

I see the code at https://github.com/django/django/blob/master/django/contrib/staticfiles/handlers.py#L31
, and it looks like
the STATIC_URL checking logic is hardcoded to the handler.

When `django.contrib.staticfiles` is added to INSTALLED_APPS, it
overrides the standard `runserver` command and use
specialized WSGIHandler to serve static files. It is not possible to
change any behavior of this handler with standard
django's request handling tools (urls.py, middlewares, views), because
handler works before them all.


>
> Reinout
>
> --
> Reinout van Rees                    http://reinout.vanrees.org/
> rein...@vanrees.org            http://www.nelen-schuurmans.nl/

doniyor

unread,
May 10, 2012, 3:54:24 PM5/10/12
to django...@googlegroups.com
if your problem is how to serve your static files, just create a folder with name static in your main app, then put all of your static files and and your STATIC_URL is /static/. 

e.generalov

unread,
May 11, 2012, 2:48:41 AM5/11/12
to Django users
On 11 май, 01:54, doniyor <doniyor....@googlemail.com> wrote:
> if your problem is how to serve your static files, just create a folder
> with name static in your main app, then put all of your static files and
> and your STATIC_URL is /static/.

Thanks. I found some more ways to get around the restrictions of the
`staticfiles` due development.

I can to execute the `collectstatic` command, to fill STATIC_ROOT, and
serve it with external server (nginx, for example),
or apply monkey patches at StaticFilesHandler to override some
protected methods.

ps: but why these restrictions are needed in the tool, which should
assist in the development?


> Am Samstag, 5. Mai 2012 15:34:47 UTC+2 schrieb e.generalov:
>
>
>
>
>
>
>
>
>
> > There was a snippet to display a content of response in browser during
> > debugging  http://miniblog.glezos.com/post/3388080372/tests-browser.
>
> > "One of the first issues you might face is seeing a style-less page.
> > This happens becuase the test server isn’t really a web server, and
> > you’re probably serving static files from something relative such as
> > '/
> > site_media'. The solution is simple: Run a separate Django server and
> > tweak your development-only static URL to something like:
>
> > STATIC_URL = 'http://localhost:8000/site_media/
> > "
>
> > but this doesn't works anymore, because django.contrib.staticfiles
> > doesn't serve static when STATIC_URL contains full URL.
>
> > I found the node at
> >https://docs.djangoproject.com/en/dev/howto/static-files/#serving-sta...
> > :
>
> > "That's because this view is grossly inefficient and probably
> > insecure. This is only intended for local development, and should
> > never be used in production.
>
> > Additionally, when using staticfiles_urlpatterns your STATIC_URL
> > setting can't be empty or a full URL, such ashttp://static.example.com/."
>
> > Is there a way to omit this limitation for local development?
>
> > (reposted from
>
> >http://groups.google.com/group/django-developers/browse_thread/thread...
> > )

Reinout van Rees

unread,
May 14, 2012, 9:29:07 AM5/14/12
to django...@googlegroups.com
On 11-05-12 08:48, e.generalov wrote:
> ps: but why these restrictions are needed in the tool, which should
> assist in the development?

I think you're using it in a strange way, as I (nor anyone I know that
uses django-staticfiles) has any problem in development.

You said in a reply to one of my messages something about "needing a
full path name '/tmp/....' for saved pages". What did you mean by that?
Such a full path is different from a proper localhost:8000 url.


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/

pedesen

unread,
May 18, 2012, 9:59:48 AM5/18/12
to django...@googlegroups.com
I have a similar problem. My STATIC_URL is a full URL like http://static.mysite.com. When I use the development server with runserver, django always takes this URL. In the templates I use {{STATIC_URL}}/myimage.jpg, and in development I always see http://static.mysite.com/myimage.jpg in my browser. When I add a static file in development it doesn't yet exist in the production static server...

I couldn't find a way to fix this, except serving my static files at www.mysite.com/static/ instead of http://static.mysite.com. But this is just a workaround and no solution to the rpoblem for me. Maybe anyone has an idea.
Reply all
Reply to author
Forward
0 new messages