Access Django App from subdomain redirect - django

I have a small django app running onn some rootserver. Its using the django integrated dev server. The url is known only to a few people so think thats ok for now. We can access the website by:
http://<ip>:<Port>/main
A colleague has set up a subdomain like shortcut.somedomain.com that points to the above url.
When trying to access the site via that subdomain the view and template are loaded, but it fails loading the static files. Is there any quick fix to make it work?

Can you show us the code that states which are the static files directories and an example where you use a static file in a template?
Is it possible you're loading the static dirs from a wrong path?
Or maybe you use refer to the files incorrectly in the template?
try doing:
my_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
this will get you the project root.
after that(make modifications if your project structure is different):
STATIC_ROOT = os.path.join(my_path, 'static')
and this should work with your STATIC_URL definition

Related

Why is Django-CMS not directly linking to my Cloudfront CDN and getting 404's for CKEditors static files?

I am trying to set up hosting for a DjangoCMS application using AWS. I'm currently utilizing Cloudfront with S3 as the CDN for media and static files, however, I've run into trouble when trying to use the ckeditor inside of the CMS plugins. I will get 404 errors that prevent it from loading at all. This is because Django CMS appears to be inappropriately building the URL's for ckeditor's static files.
The urls look like this:
http://example.com/en/admin/cms/page/add-plugin/my.cloudfront.net/static/djangocms_text_ckeditor/ckeditor/skins/moono-lisa/editor.css/change/
When not using the CDN and storing the files locally it looks like DjangoCMS creates a 304 redirect to the proper location, but when using a CDN it seems to be mangling the url for some reason.
Is there anyway to configure the way DjangoCMS is building this url, or some standard way to implement CDN's for DjangoCMS that I may be mishandling? The current way I have the CDN setup is to have Django redirect static and media requests to the CDN.
I fixed it!
You just need to override the "TEXT_CKEDITOR_BASE_PATH" in settings.py.
TEXT_CKEDITOR_BASE_PATH = 'https://%s/djangocms_text_ckeditor/ckeditor/' % STATIC_URL

Can I have an organized way to work with django?

I come from an Mean, Express, Angular and Node (MEAN) stack background. I really liked how angular has separate components and then we can style each components and then nest them in each other. It is just more organized and way more shareable when in companies. It gets easier to actually understand the structure. Can I do this in django since I notice that my css is supposed to be all in one static file. Is it possible?
NOTE: Angular not Angular.js
While all your static files do need to be contained in one folder in production, they can be in separate places in development.
Development: Separated
You can have separate static files for each app within the project, but each should contain a folder with the name of the app. So the path to the static file style.css in the app myapp would look like: myproject/myapp/static/myapp/style.css. You can also put more folders within the myproject/myapp/static/myapp to separate images, JS, and CSS, for example.
To have this work you need to make sure that django.contrib.staticfiles is in INSTALLED_APPS. Then make some edits in settings.py and set STATIC_URL = '/static/' and STATICFILES_DIRS=[os.path.join(BASE_DIR, 'static')], so that Django knows the URL to refer to as well as the folders to check for static files.
Production: Collected
The above will only work if DEBUG is set to True, so it will only work in development. To have it work in production, you need to set STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles'), so that Django will know to look in myproject/staticfiles to find static files.
Then just run python manage.py collectstatic and Django will go through all your apps and pull everything from the folders called static within them and place everything in STATIC_ROOT.
This is why it's helpful to use the convention of putting a folder called myapp within static in myapp—because they all end up in the same folder.
See the docs for more: https://docs.djangoproject.com/en/2.0/howto/static-files/
We can use the webpack method. Could not find this tutorial easily on googling.

hostgator not recognizing urls.py of Django project

I am working within the File Manager of Hostgator and have already set up a Django project which displays "It worked! Congratulations on your first Django-powered page." Following this, I ftp'ed my local Django project (which works fine on the local server) into File Manager and stored it into a temp folder, copying over the changes to the urls.py, views.py, and settings.py files of the new Django project along the settings path.
However, nothing appears to be changing on the website - it's still on the "Congratulations" page. I've tried moving various templates into different places to see if it responds, but that doesn't appear to be doing the trick. Thoughts?
You need to read documentation about how to manage your project on the hostgator servers

What is the best practice for serving static files in Django currently

I've found plenty of advice for how to tackle static files in Django 1.x. Is there a best practices way to go about doing so?
There are may approaches to serving static files in Django, but Django 1.3 introduced a new option to handle them. Basically, you can define specific directories at a project level or at an app level that contain static media. Then via a management command, 'collectstatic', you can copy all of the static files in your project directory to a separate directory (likely external to your project) that is served by your webserver. This solves a lot of complications of the previous approaches.
1) It allows 3rd party apps to easily include static files in a standard way. No more creating symlinks from your webserver directory to locations inside of individual python/django modules.
2) It gives you more control over where your webserver can host its static files. For example, you can define all of your static media inside of you project under version control, but then copy it all to an external location anywhere on the filesystem. This prevents having to point your webserver to a location inside of your project.
3) Is splits out static media that that will never change from static media that is uploaded by a user using something like a FileField. This is good because you likely want to keep site-level static media in version control (to be installed on your dev servers), but user-submitted content will upload to a different directory (unlike versions of django prior to 1.3).
I think this functionality used to be a 3rd party module that was merged into core.
Here are the docs:
https://docs.djangoproject.com/en/dev/howto/static-files/
Basically there are a few new settings.py variables:
STATIC_ROOT - This is a directory on your filesystem that will be served by your webserver. When you run ./manage.py collectstatic, all of the static files from your project and it's apps will be copied into the directory that you specify here.
STATIC_URL - This is the url that you will set to represent what url base your content will be based from. If you set this value to "/static/", then "/static/" will prefix all urls of static media that included. You will also use this variable in your templates. For example, in your template, you could specify something like:
<img src="{{ STATIC_URL }}logo.png">
The STATIC_ROOT settings variable specifies where static files are copied to, but you also need to provide a location of where static files are copied from. This can be done by creating a directory in your individual Django apps called "static//", similar to what you would do for templates within an app. Upon running the collectstatic command, Django will copy all of the static files from all of the "static/" directories in all of your apps to the STATIC_ROOT directory. You can also use the STATICFILES_DIRS settings variable to define project-level directories to copy static media from.
Though there are many ways to serve static media, I think this is a nice api set in place by Django that will help with better integrating 3rd party modules. The fact that this feature is now included in core could give some indication that this approach may have gained some momentum.
Hope this helps,
Joe

Changing default url to static-media in Flask

I've made a website using Flask and I have no problems getting things to work properly on the built-in development server. I've also been able to get things running on my production server under mod_wgsi. However, I host my static media from a static/CGI/PHP-5.2 application and I can't get Flask to 'see' it without manually changing all the urls in my html files.
The problem seems to be that the basic Flask setup expects static files to be within the flask application. See here for details. Essentially, I think I need to change the url of 'static' portion of the following one liner:
<link rel="stylesheet" href="{{url_for('static', filename='css/print.css')}}" type="text/css" media="print"/>
It looks like I can change this in init.py, instructions here, but defining the static_path as follows doesn't seem to work.
app = Flask(__name__, static_path = '/web_media')
To be clear, if I manually define my url like this:
<link rel="stylesheet" href="/web_media/css/print.css" type="text/css" media="print"/>
everything works fine. Any help would be greatly appreciated.
Override static_folder instead.
app = Flask(__name__, static_folder = '/web_media')
If this is a production set up Flask should not be serving your static content; the web server (nginx, apache, cherokee, etc...) should be handling that as it is more efficient at handling those types of operations than the python process. From the sounds of it (based on the mod_wsgi reference) you are using apache, so this is how you could alter your config file to serve static content from static/CGI/PHP-5.2 using apache.
Assuming "web_media" is a directory under the somewhat fictitious /var/www/static/CGI/PHP-5.2 directory and contains your css/js/etc. assets. In your config file, within the area where you configure this app add something along the lines of.
Alias /web_media/ /var/www/static/CGI/PHP-5.2/web_media/
<Directory /var/www/static/CGI/PHP-5.2/web_media>
Order deny,allow
Allow from all
</Directory>
It's been a while since I used/configured apache, so no guarantees that the above will work perfectly the first time. But the main point is, use the webserver to handle the static media.
For production environment you should serve your static files via http server (e.g. nginx/apache). See answer of Philip Southam above.
Nonetheless, there are times when you use werkzeug server (e.g. dev/testing). In that case, there are 2 keyword arguments to look at:
static_folder: physical location on the file system. If it is relative path than it is relative to the project root directory.
static_url_path: this is the url lookup path for static content.
For example, if I have a static file located at /path/to/static/files/myfile.jpg, with /path/to/static/files as static file root and you want to access it via http://example.com/mystatic/myfile.jpg. I will do the following:
from flask import Flask
app_name = __name__
app = Flask(app_name, static_folder='/path/to/static/files', static_url_path='/mystatic')
Assuming you are using flask version 0.7 or above (currently 0.11.1)