Is it possible to use the static media files from a development server locally?
Instead of linking locally to localhost/static/media/some_pic.jpg I would like to link to use our development server dev.site.com/static/media/some_pic.jpg when I use relative urls in the templates.
You can just change the MEDIA_URL in your settings to 'http://dev.site.com/static/media/'.
Related
I've built a Django project with Python and a MySQL database. I'm ready to deploy it to a shared server hosting platform called Hostgator. Their tech support tells me to load all my project files directly into a public_html directory, but when I do that, and navigate to my domain, I just see a list of files (see below), instead of the website I built. What am I missing?
I can't find any good documentation for this kind of deployment. I've done the Django deploy checklist and I think I have that stuff done right. I'm wondering about if/what to put in an .htaccess file, and I'm also not sure how to configure my STATIC_URL or STATIC_ROOT. Do I need to update those to have the path of my production domain? I have run the collectstatic command on the project.
As of now, I have the following for my static file handling in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
You don't want your django app in public_html. If you are using nginx or apache (you are using one of them, probably visible in the lower left of your screenshot just out of crop range), you likely want to proxy to the process running your Django app (gunicorn is one way to do that).
Essentially, Nginx handles all the web traffic, and hands off (via proxy) anything for your Django app to Gunicorn which is running your wsgi application (Django). Nginx can also then serve up your static files as well.
Digital ocean has a decent 'how to' that covers most of it in depth.
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
I'm getting an error when trying to serve static file on IIS using FastCGI with Django==2.2.5 and python==3.7.4
my settings.py
static code
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I'm using wfastcgi version 3.0.0 and my server is windows server 2012 datacenter R2.
I tried all solution here Django Admin Page missing CSS in IIS and here How to fetch static CSS files with django on IIS? and watched some tutorial on youtube but still stuck with that.
The error when it shows when I'm trying to access the admin page
GET http://xxxxxx/static/admin/css/base.css net::ERR_ABORTED 404 (Not
Found)
It's difficult to figure what your environment situation may be, but you may me missing a Virtual link to your static directory in IIS. Also, you will need a web.config in your static directory. Your project will require a total of two web.config files.
Here is an example of what your static directory should look like https://github.com/Johnnyboycurtis/webproject/tree/master/static
You can review that project and follow along in the tutorial Deploy Django on Windows using Microsoft IIS
I have configured my django application on webfaction, and it is working fine as of now. I wanted to know how to serve my static and media files on apache. Django has a development server which serves these files. I had initially setup apache on my local environment and added many lines of script to serve static and media files via apache.
However, while doing the same on webfaction, as per the webfaction docs I didn't have to do much and that got me thinking that there might be more steps needed to serves these files via apache.
Therefore, I wanted to confirm, are my static and media files running from apache or not? I am afraid that this is the case here as well because I have not yet configured apache to serve these files since in development environment django serves the static and media files are served by django itself.
A typical Django deployment at WebFaction involves at least two application instances:
A Django app instance for the version of Django you're running
A Symbolic Link to a static-only app instance
The Symbolic link to a static-only app expects you to enter the absolute path for the origin of the symlink. This should point to the directory specified by STATIC_ROOT.
Given an project structure of:
/my-project/
/my-project/
settings.py
...
/static_assets/ (used for local development. Not typically deployed.)
/static/ (used for production)
This would translate to a directory structure at WebFaction like:
/home/
/your-account-name/
/webapps/
/your-django-app-name/
/apache2
/bin
/your-django-app-name (synonymous with "my-project" above)
/static/ (this is what your 'static' symlink path)
/lib
/pythonx.y (modules go here. Django lives here)
When you create your "Website" instance, you will specify your Django app instance to run on /, and your Symbolic link app instance on the path specified by STATIC_URL, typically /static.
You should run with DEBUG = False at WebFaction, which in turn, causes django.contrib.staticfiles to not serve any files that are located in STATICFILES_DIRS.
Hope that helps you out.
I have a Mac running OS X 10.9.3. I am trying to setup a Django application backed by a PostgreSQL database served by gunicorn, with static assets served by NGINX. I'm an old hand at Django with MySQL running with the developement server (manage.py runserver). But I'm new to setting it up with virtualenv, gunicorn and NGINX. So I'm following the instructions here.
My Django Project is being served successfully at http://localhost:3026. As a test of the database connectivity, I wanted to take a look at the Django Admin interface. I visited http://localhost:3026/admin/. I have included a screenshot below.
Why does this admin page look so ugly? It lacks the neccessary graphical interface and css that it is supposed to have? It looks like NGINX is not properly serving up those static assets. How can I troubleshoot and fix this issue?
I even did python manage.py collectstatic. That went and successfully copied all the static files to where they were supposed to (I think?) live in /opt/myenv/static. You can see the output of that command here. I then re-started gunicorn and nginx. I thought that would fix it. But unfortunately it didn't. The issue remains. In my Django settings.py file, I have configured the STATIC variables as follows:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
)
STATIC_ROOT = "/opt/myenv/static/"
STATIC_URL = '/static/'
Static files are only served by Django when the debug server is being used. When the site is being handled by a separate web server you need to configure the web server to serve the static files itself.
Actually, it is recommended to use Django's own development server(python manage.py runserver) while you are on localhost(from my years' experience with Django and virtualenv). Since it save your precious time configuring the nginx locally.
However, if you do want to use Nginx to serve you static files locally, you may want to copy django's contrib **/admin static file directory to where your virtualenv points to**. Your virtualenv file(like a *.nginx file) of nginx will look like this:
server_name localhost;
location /static {
alias /path/to/where/your/admin/static/directory/is;
autoindex on;
access_log off; #optional
}
Then restart nginx you probably will get it working.
PS: for more about installing and configuring Nginx locally on a Mac, you may want to check this out.
I'm deploying my app and I wonder what I'm missing.
I did the following:
Set my STATIC_ROOT to an empty folder in my server.
Set the STATIC_URL to '/static/'
Added 'django.contrib.staticfiles' to INSTALLED_APPS
In development my static files are in the root of my app in a folder named static.
So, I ran manage.py collectstatic and all my files were copied to my static_root.
However, it doesn't work. I don't know if i'm missing any step.
Any help would be great
Thanks
. 4. Point Apache at your static folder.
As explained in Django's documentation, Django serves static files itself in development only, when deploying your application in production, it's up to you to make your web server (apache, lighttpd, nginx, whatever) serve the static files.
Django's documentation provides instructions for doing so with Apache here
Django while it's not in debug mode should not serve static files, for performances reasons, you should use:
./manage.py collectstatic
then configure your web server (apache or nginx) to serve this folder to the right url.