estoy corriendo Django 1.3, Python 2.7 en Windows XPProblemas al enlazar a archivos estáticos en Django 1.3
Estoy intentando configurar un css en una carpeta estática para mi aplicación Django.
La plantilla se parece a:
<html>
<head>
<title>css demo</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/my.css" />
</head>
<body>
El HTML generado se ve así:
<html>
<head>
<title>css demo</title>
<link rel="stylesheet" type="text/css" href="http://localhost/static/css/my.css" />
</head>
...
Así que parece que tengo todo lo establecido con el fin de especificar el lugar donde los archivos estáticos están en la plantilla y luego en el html generado
Pero no aparece en 'http://localhost/static/css/my.css'. ¿Cómo lo consigo?
me encontré collectstatic así:
C:\root\workspace\mywebapp\src\mywebapp>c:\Python27\python.exe manage.py collectstatic
You have requested to collect static files at the destination location as specified in your settings file.
This will overwrite existing files.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying 'c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css'
1 static file copied to 'C:/root/workspace/mywebapp/src/mywebapp/static/'.
Así que ahora tienen my.css en c: \ root \ espacio de trabajo \ mywebapp \ src \ mywebapp \ css_demo \ estática \ css \ my.css
en la configuración de mi, tengo:
STATIC_ROOT = 'C:/root/workspace/mywebapp/src/mywebapp/static/'
STATIC_URL = 'http://localhost/static/'
y en mi url.py tengo:
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
(r'^mywebapp/', include('mywebapp.css_demo.urls')),
)
urlpatterns += staticfiles_urlpatterns()
Así que si he entendido bien, mi css en:
c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css
debería de estar disponible en:
http://localhost/static/css/my.css
Pero en lugar de eso ver:
Page not found (404)
Request Method: GET
Request URL: http://localhost/static/css/my.css
Using the URLconf defined in mywebapp.urls, Django tried these URL patterns, in this order:
^mywebapp/
The current URL, static/css/my.css, didn't match any of these.
También probé:
http://localhost/mywebapp/static/css/my.css
http://localhost/mywebapp/css_demo/static/css/my.css
¿Me estoy perdiendo un paso aquí? La documentación sobre esto es un poco confusa. http://docs.djangoproject.com/en/1.3/howto/static-files/ http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/
¡Gracias!
hmm ... no Lo inicié en el puerto 80 con runserver 0.0.0.0:80 –
La URL absoluta no solo no es necesaria, pero no funcionará en absoluto. Si establece STATIC_URL = 'http: // localhost: 8000/static /', producirá errores extraños, como pipas rotas y 404s para los archivos estáticos. La razón por la que sucede esto es desconocida para mí, pero un poco acerca de este comportamiento se menciona en los documentos en https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in- desarrollo –