How to serve another static root in Django for html pages? - django

The typical configuration serves two static roots:
http://www.example.org/static/
http://www.example.org/media/
This is STATIC_URL and MEDIA_URL.
I would like to add a third one to host static files build with Sphinx:
http://www.example.org/docs/
I know I could configure this on the level of the web server. Is it also possible to configure this on the level of Django?
Here is my python package, that implements Sphinx with Django templates and renders it to static pages. A kind lightweight read-the-docs. Still in an early state, yet working.
https://github.com/elmar-hinz/Django.SphinxCMS

How about building a url just like you do for the static files? The DOCS_ROOT setting should be a string in your settings.
from django.urls import re_path
from django.views.static import serve
from django.conf import settings
urlpatterns += [
re_path(r'^docs/(?P<path>.*)', serve, {'document_root': settings.DOCS_ROOT})
]

Related

Django/Wagtail Media Files not showing up in the admin portal on a fresh install

I am working on a site in dev that contains a media folder. When I do a fresh setup of the site (empty db) and do all the migrations and steps to get the site up and running I noticed in the admin portal none of the images and assets in the media folder dont show up even though they exist. I have to re-import an image and then it shows up in the admin portal as expected. I have looked all over and cannot find an answer. To put it simply why isnt the admin portal importing existing files in the media folder on a fresh setup?
django==3.2
wagtail==3.0
base.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'
urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.contrib.sitemaps.views import sitemap
from search import views as search_views
urlpatterns = [
...
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If anyone has any idea?
That is totally normal. Just as you need to import your page data into the database, you also need to import the information about your documents into the database (in addition to having the files). You can write a script to help with these imports. I don't have one for images but here is one I wrote for importing documents from a nested directory in the file system into a nested set of collections: https://gist.github.com/cnk/54031ca6775fa0d29997449a1e2010ec

Pycharm unresolved references with Django

This is my 5th day learning Django. I've got a Django project working in a virtual environment using Pycharm. The problem I have is, Pycharm can't reference certain imports when they are actually working in Django without any issues. I've selected the right interpretor for the project but Pycharm cannot connect the imports with the .py files in the Django project. The selected interpretor does have Django for the virtual environment but it still doesn't work. Below are the screenshots:
import works in Django but Pycharm cannot resolve reference with the correct interpretor
You need to mark the project's root source. You can do this by right-clicking:
Or by going to PyCharm settings > Project: Project Structure > and make sure to mark the project root directory (the folder that holds your project's apps) as a "Sources" directory.
Sidenote: I noticed you're not using directory-level urls.py. I would suggest doing that instead of trying to import URLs from other apps into the project-level urls.py.
What this means is, each app - articles, accounts, etc. - would have a file named "urls.py".
In your project-level urls.py, you do this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
path('accounts/', include('accounts.urls')),
]
In your app-level urls.py, you do this:
from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name="hey"),
]

How can I host a Django web app on Cpanel out the public_html folder?

I am trying to host a Django web application on Cpanel.
However, my hosting service is having a main folder which is called public_html. In this folder, there is the index page. My project folder named myapp is out of the public_html folder. Whenever I run the application, it is showing the content of the index.html which is in the public_html folder instead of running the home page of my application that should be executed from this main urls.py file.
Bellow is the main urls.py content.
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('depenses/', include('depenses.urls', namespace='depenses')),
path('cart/', include('cart.urls', namespace='cart')),
path('orders/', include('orders.urls', namespace='orders')),
path('coupons/', include('coupons.urls', namespace='coupons')),
path('', include('shop.urls', namespace='shop')),
# path('', TemplateView.as_view(template_name='templates/index.html'), name='home'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Again my project is out of the public_html folder.
Please assist me to host my application.
This is normal behavior considering the fact that the web service will attempt to serve HTML/PHP files only.
You will need to deploy your Django app through a python interpreter like Apache Passenger or something similar that would handle and serve it.
This can be easily achieved without many configurations and hassles with the Python App feature of CloudLinux.
Therefore, I would suggest you simply find a hosting provider that provides CloudLinux's Python App feature. The deployment process via that feature is with just a few simple clicks.

Why doesn't Django serve static files when the dubug=False in settings?

when I tried to run the project, static files are missing, and the settings.debug is set to False.
Django does not serve static files in production (when DEBUG = False) because doing so would be in their own words (As stated in the section Serving static files during development of the documentation):
This method is grossly inefficient and probably insecure, so
it is unsuitable for production.
Why is this inefficient one might say? Well static files are mostly larger in size than your normal HTML files, plus a website is bound to have lots of static files. If Django served static content even in production much of the time of the server would be wasted in serving these static files. Plus to serve multiple requests at the same time we run multiple Django processes simultaneously, if there are many requests for static files this will cause the processes to waste time serving them, causing other requests to wait if there are no free processes.
Plus as #Reda Bourial mentions in their comment Django doesn't handle compression well (One would want to compress their static files so that less bandwidth is required, both by the server and the client). Furthermore Django should focus more on the task it is designed for, which is rendering the pages requested by users (A CPU bound task), whereas serving static files is mostly just an Input / Output (I/O) task and for Django to spend time on these tasks (even when it is not efficient at them) is clearly a waste.
Servers like NGINX or Apache can serve static files much more efficiently, hence it is best to use them in production instead of having Django do it. For details on how to configure static files for production see Deploying static files [Django docs]
Actually in Django, while you run your project on the development server it won't serve your static files when you try to serve the static files after changing DEBUG settings to False. It will work properly on the production or stagging server but not on localhost server.
Still, there is a way to do this you can get by running your development server on insecure mode.
Try this,
python manage.py runserver --insecure
it is not recommended, but if you are just getting started and want to understand more about what's going on, then you can also link directly:
urls.py:
from django.conf.urls.static import static
from django.contrib import admin
from djangoapp import settings
from webtools.views import *
from django.urls import path, include
"""
direct link on the server in production
"""
from django.views.static import serve as mediaserve
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('webtools.urls')),
]
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else:
# direct link on server in production
urlpatterns += [
url(f'^{settings.MEDIA_URL.lstrip("/")}(?P<path>.*)$', mediaserve, {'document_root': settings.MEDIA_ROOT}),
url(f'^{settings.STATIC_URL.lstrip("/")}(?P<path>.*)$', mediaserve, {'document_root': settings.STATIC_ROOT}),
]
settings.py:
DEBUG = False
.
.
.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = []
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Django Displaying images uploaded by ImageField properly

I'm building my own porfolio using Django, and I just had a question regarding uploading images using the ImageField.
I uploaded an image through the admin page using ImageField, and after a long search session, finally got my page to display the image successfully.
urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',include('pages.urls')), # main landing page
path('admin/', admin.site.urls),
path('project/',include("projects.urls"))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
projects.html
<img src="{{project.image.url}}">
However, the official django docs say that this is not a good way to deploy a django site. Why is that, and what is the best/proper way to display images?
The reason is also given in documentation:
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
Proper way of deploying media files in production is to use a reverse proxy server like NGINX or Apache, or you can use a S3 compatible storage(like Amazon S3) to serve those media files as well. I can give you an example configuration in NGINX for serving media files:
location /media {
alias /path/to/media/direcoty; # Change to your own media directory here.
access_log off;
}