How to relocate the django admin site below root? - django

The contrib admin site of Django is a perfect starting point for simple in-house applications that lots of people use.
I've done a bit of searching and seen the discussions of media and tools for working with and merging static media for the admin site. But it is still pretty fragmented and opaque.
Assume I have a site "mysite" with an application "myapp" that is derived from the admin app.
I would really appreciate an example of relocating mysite and myapp, including the admin app, onto a web server where it is not at the root url.
I think this use case would flush out a lot of the issues and confusion that I see people have.

I think what you want to do is use the FORCE_SCRIPT_NAME = '/path/not/at/root/of/domain' option in your settings.py file. No trailing /.
http://docs.djangoproject.com/en/1.3/ref/settings/#force-script-name
I hope I'm understanding your question right and I hope this helps solve it.
Edit: oops gave link to old docs...updated with new docs version
Edit2: Here is another link describing this here at djangoproject again.

Related

Self-hosting multiple sites via one Django project + apache - Django Sites framework

I have a self-hosted (raspberry pi) website running on Django - one project with several apps all serving on one domain name. I'm more of a programmer than a server admin, but it works! Now, I'd like to break one of those apps out onto a second domain name, but still hosting from my pi.
--I've looked into a lot of tutorials on how to do this as well as checked out answers like this one but the server-related instructions on everything I've found so far all go a bit over my head or don't seem to match up with my setup.
--I've tried the Django Sites framework and I've also followed this tutorial. While I was able to get my site objects in the db, all requests seem just to go to SITE_ID=1, regardless of domain name. I've seen conflicting information here about whether or not SITE_ID should be present in settings.py, but whenever I remove it, I just get errors about it (even though I do have the CurrentSiteMiddleware installed as well as a custom middleware like indicated in that tutorial).
Here's what I've got for Django (3.2.14, and py3.7.3), essentially:
DjangoApps
-mysite
--settings.py (allowed_hosts includes mydomain)
--urls.py (imports from app urls files)
--(etc)
-app1
--urls.py
--(etc)
-app2
--urls.py
--(etc)
-app3
--urls.py
--(etc)
and then the server confs:
/etc/apache2/sites-available/
-000-default.conf
-default-ssl.conf
-mydomain.com.conf [which points to the DjangoApps directory]
-mydomain.com-le-ssl.conf
I do not have an http.conf as mentioned in many of the tutorials/answers I've read through. I've seen some notes about using virtualenv and/or wsgi but I don't understand them, and I am requesting a more handholding step-by-step explanation of what to do here.
I assume this isn't the most optimal/modern way of doing this, but the key point here is that this main site is already live and working as-is so I'm really not looking to redo the whole thing. All I want to do is make it so that myseconddomain.com serves app3 only, and mydomain.com keeps serving everything (or, everything but app3, if that's easier).
Please help! And please let me know what other info is required.

Django Authenticate against external SSO

Forgive me if the wording here is a bit odd. I have a fair amount of experience with python, but I'm pretty new to Django and still trying to get my sea legs.
I've inherited a PHP site from the former System admin at my new job, and I want to rework it into a django app to simplify/cleanup the code.
The PHP site is using .htaccess file with Pubcookie apache module for authentication (as well as some basic checks in the php code to ensure the user checks out against a database of authorized users, but that part i'm less concerned with), and looks like this:
AuthType SecureID
require valid-user
PubcookieAppID "name of the app"
In my apache configs, I have various pubcookie definitions.
LoadModule pubcookie_module /usr/lib/apache2/modules/mod_pubcookie.so
PubcookieGrantingCertFile /usr/local/pubcookie/keys/pubcookie_granting.cert
PubcookieSessionKeyFile /etc/ssl/private/akey.pem
PubcookieSessionCertFile /etc/ssl/certs/acert.pem
PubcookieLogin https://weblogin.domain.com/
PubcookieLoginMethod POST
PubcookieDomain .domain.com
PubcookieKeyDir /usr/local/pubcookie/keys/
PubcookieAuthTypeNames ADUserID null SecurID
My question is...How do I implement this sort of thing on the python end? I have looked at using RemoteUserMiddleware, or PersistentRemoteUserMiddleware.
But most of the information I have found for that doesn't go into great detail about how to actually point your app at the correct login page. The official documentation seems to stop at adding the middleware under middleware in the settings.py file in the django project.
It also bare's mentioning that the weblogin.domain.com is handled by another group that I don't have direct access to, and is not one of my servers so I can't really poke around there.
I realize this is...extremely broad, but any information that could point me in the right direction would be appreciated.
In an ideal scenario, I would like to be able to restrict different views to different authtypes, but I'd be happy to get anything working sensibly.
So, For what it's worth. The answer was to use pubcookie with apache as normal. then use RemoteUserMiddleware or PersistentRemoteeUserMiddleware as expected. As it turns out you don't have to handle any outside configuration for the middleware, it just imports whats shoved at it.
Finally the piece I was really missing, information from apache/pubcookie authentication can be accessed with request.META library from inside django code

django admin django.contrib.staticfiles

I'm following the tutorial contained here:
http://www.djangobook.com/en/2.0/chapter06.html
They say that the admin site should look like this:
http://www.djangobook.com/en/2.0/_images/admin_index.png
When I start the admin site, though, it looks really simplistic, just plain text and links:
Django administration
Welcome, admin. Change password / Log out
Site administration
Auth
Groups Add Change
Users Add Change
Recent Actions
My Actions
None available
I noticed that it looks all nice like the link when I uncomment django.contrib.staticfiles from the INSTALLED_APPS, although that wasn't mentioned in the tutorial...can someone please explain this behavior to me?
Thank you for your help!
The Django Book is a little out of date (although an update is in the works I believe):
This book was originally published by Apress in 2009, and covered Django 1.0. Since then, it’s languished. We’re working on getting the book updated to cover Django 1.4, 1.5, and beyond
Static files are all the CSS/JS & images that your site (and the django admin) uses. They need to be collected and placed somewhere that your server (or development server) can serve them. This is the job of django.contib.staticfiles.
You can read more about this in the 'Managing Static Files' documentation
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them.

My Apache (mod_wsgi) Django app only lists the files instead of the actually running website/application

My Apache (mod_wsgi) Django app only lists the files of my website/project instead of the actual running website/application. My guess is that it's mod_wsgi that is the culprit but I'm not really sure. What are some of the causes of this?
Don't put your Django site code in a directory Apache can serve up. Right now if I knew the actual site host name, I could get down your Django settings file and get access to your database password information.
Anyway, ensure you have gone and set up a basic WSGI hello world program before you attempt to get Django working under mod_wsgi. In doing this, ensure you use the official mod_wsgi documentation and not some arbitrary persons blog post. As such, go read:
http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
Also go watch the video presentation at:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
as it steps you through basic configuration as well as what to do for Django. It covers all the basic things that people do wrong.
If you still can't get it to work, then heed the comments in:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Asking_Your_Questions
about useful information you should supply to any forum to help people solve your problem.

libjpeg PIL photologue django

So I have been looking up and down for a good solution to display images, audio, media in general with django:
From what I found there is the following solutions:
1. Photologue:
Cant seem to make it work.
It needs PIL and libjepg.
I tried to install both, but ran into different build problems.
Someone on stackoverflow recommended me this link:
http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/
Which is a great tutorial and also deals with removing PIL and reinstalling (which I have to do, I guess).
But somewhere along this tutorial I have to do this:
sudo rm -Rf build
And I dont feel confident enough to run a sudo rm command.
Also I thought there must be an easier way to achieve this, since EVERY Blog needs to display media.
2. Django itself says:
Let Apache do the display and use this:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/pathtomedia/', 'show_indexes': True}),
for development BUT not for production.
So what does everybody use for production?
3. Other:
I managed to display images via os.listdir , urllist and a template:
allimg= os.listdir("/pathtomyimages/")
urllist = ['/site_media/Imagedir/%s' % url for url in allimg]
return render_to_response('picture_display.html',
{'allimg': allimg})
But this also seems not very promising, since I want to display images, audio, media from one directory on multiple sites.
I hear paginator could be the solution.
So now my question:
What is the best, easiest and robust solution to display media in django in development and in production?
What are you using?
Please help me out!
Thanks for the time.
django-photologue is an app for managing images and image collections for your project but isn't directly related to serving static media.
Since "best", "easiest", and "robust" are all fairly subjective terms depending on your specific needs, I recommend you first seek to understand why it is not common practice to serve your media files from Django. The short, generic answer is that Django must be loaded into each web server process that handles a request and this will use a non-trivial amount of memory. When serving a media file you (usually) just return a file from disk. Since no application logic is required you are incurring unnecessary overhead both from the memory required to run Django in a process and the extra code processing that will occur for Django to map the request path back to a view, load the file, and dump it into the response. Web servers (Apache, Nginx, etc.) are already very good at returning files from disk. This is why it is recommended that you have your web server handle static media files. The request does not ever need to get handed off to the WSGI middleware or Django.
There are many options available but there are a few that are likely to be common solutions. For example, if you've decided on Apache as your web server, you would need to configure it so that requests to your media path are handled directly by Apache and all other requests are handed off to Django. Apache + mod_wsgi is a fairly common choice AFAICT. Read through this carefully:
http://docs.djangoproject.com/en/1.2/howto/deployment/modwsgi/#howto-deployment-modwsgi
It is not strictly necessary to configure a separate web server as the documents recommend. You can use the same Apache instance for Django and media as you're getting started.
That tutorial is actually mine :)
the sudo RM -rf build, all this does is actually force remove any previous builds, which basically means it clears anything to do with the builds.
hope this helps