Use sphinx with Flask and preserve formatting - flask

I generate some Python documentation with Sphinx. If I go to the build/html directory and open index.html directly, I get Sphinx formatting.
If I try to serve the same page Flask with the following code, I get the page but all the Sphinx formatting is gone:
from flask import send_from_directory
#app.route('/help')
def help():
return send_from_directory('/sphinx/build/html', 'index.html')
I am calling Flask directly with app.run in debug mode. This is how I like it. I know how to set it up in Apache but I don't want to run Apache. Is it possible to serve Sphinx inside Flask and still get the formatting, or do I absolutely have to run a web serve like Apache?

Copy your Sphinx documentation folder with generated html files /sphinx/build under the static folder ./static/sphinx/build.
Then use Jinja to link it:
Documentation
The resulting url on your local server will look similar to this:
http://127.0.0.1:5000/static/sphinx/build/html/index.html
When your app is deployed, replace the new directory with a symbolic link, so needn't copy the /sphinx/build folder every time the documentation is built.

Related

How to correct Django server from loading basic html on web browser instead of standard html?

How can I correct the basic html display I get when I load the Django admin page, just as shown in the snapshot attached?
This is because your static files aren't set up properly. Typically Django's development runserver will take care of this for you, but in production, you need to run python manage.py collectstatic after ensuring your static files settings are correct: https://docs.djangoproject.com/en/1.11/howto/static-files/#configuring-static-files
Good luck!

django rest swagger does not display properly

I want to use django-rest-swagger to document my APIs, so I follow the official doc to setting in my django app. But it doesn't display properly like below:
I was using Nginx+supervisor+gunicorn to serve my django app. Is it possible that they cause it?
The proper page should be like below:
(source: django-rest-framework.org)
I assume that your static files is not configured. You can see this in firebug or chrome dev tools.
Read the documentation how to deploy static files (https://docs.djangoproject.com/en/1.8/howto/static-files/deployment/)
Check that rest_swagger in your INSTALLED_APPS.
Check that STATIC_URL, STATIC_ROOT settings is setted properly.
Call collectstatic management command.
Check that nginx configured to serve static files in STATIC_ROOT folder.

django login screen (default setting) doesn't display properly?

My django admin page didn't display properly. Like a pure html display. Can someone help here?
I don't have the permission to upload screenshot. But you can imagine, it doesn't look the same as tutorial shows.
thanks
You probably don't have static files serving set up. If you have DEBUG turned on, you'll get this automatically with Django's development server (./manage.py runserver). If running in production, you need to set this up manually. See the docs for more info.
Basically your static files are not set up properly, some of the reasons can be :-
If you running on a server like apache, you need to have static url in settings.py and same alias in httpd.conf file of apache.
If you are running on django in built server, please try to set Debug=True, which will force django to serve those static files.
If you have debug=False in settings.py file, https://docs.djangoproject.com/en/dev/howto/static-files/
Because u really need a static files from django, sometimes you have to set the alias of django admin static files separately, which can be set in urls.py that is pointing to django package
found the issue.
I forgot to turn on
'django.contrib.staticfiles'
in setting.py file

Django url template tag incorrect for deployment subdirectory when using collectstatic

I am deploying my django project under a subdirectory of my site, e.g.
http://example.com/apps/[django urls]
The problem is when I run collectstatic, a particular plugin I am using (dajaxice) uses the {% url %} tag to create the appropriate javascript code. Since collectstatic doesn't know about the deployment subpath, the reverse lookup is to the root url instead of the subpath. For example, it should be:
/apps/dajaxice/my_func
instead of:
/dajaxice/my_func
Is there a good way to change the way collectstatic does the reverse url without hacking the plugin? The only thing I can think of is to have one url specification for collectstatic that includes the 'apps' subpath and another one that does not for everything else. However, I cannot figure out how to change the settings.py when using collectstatic only. Any suggestions or alternative solutions?
I finally found how to solve this problem. Dajaxice provides a setting to change the url prefix:
DAJAXICE_MEDIA_PREFIX = 'dajaxice'
By defining this setting, to include 'apps' in the subpath, we can get the url we need. The problem is this prefix must only be altered in the collectstatic command and not when serving a webpage. Therein is the rub.
The solution is to create another settings.py file, we'll call it settings_cli.py. It will look like:
from settings import *
DAJAXICE_MEDIA_PREFIX = 'apps/dajaxice'
Next, we must load the new settings file when executing commands only by changing our manage.py file. Line 6 will now read:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MYPROJ.settings_cli")
where it formally refered to "MYPROJ.settings".
Now, when we run:
python manage.py collectstatic
it will use our special prefix, but not affect the normal prefix needed when serving webpages.
For more info on mulitple settings files, a good reference is http://www.djangobook.com/en/2.0/chapter12.html

admin site looks different in production environment

so I'm building a basic site to learn django, and i am currently setting up the admin site. when launch the site through
python manage.py runserver
i can go to the website and it looks pretty just like this. http://cl.ly/2Z3A2n0I1E140X3k180h
but when i load the site through my apache web server it looks like this. http://cl.ly/3H3r1G2D193p462t3641
can anyone help me?
it looks to me like the admin site is not able to load the proper template on the live site. but i did not setup any special template. and when i look at the permissions of the template folder it seems to be fine... maybe not. any help?
Ok, sorry for the long story:
First, find out where your django installation rests:
in the CLI:
>>> import django
>>> django
<module 'django' from '/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'>
The this is the path (except for the __init__.pyc, of course) to your Django installation.
Now, in your media directory, you could create a symbolic link to the directory that the admin media is located in (this way you will not have to copy the files to your media directory).
Assuming that you are in your media directory use this command to create dynamic link to the admin media directory (the first argument to ln -s should of course be your django path that we got retrieved earlier):
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/ admin
Now in your settings.py you can use something like this:
MEDIA_URL = 'media'
ADMIN_MEDIA_PREFIX = '/media/admin/
You will also have to add this directive to your Apache config:
Alias /media/ /full/path/to/your/django/site