Django plugged into Apache not working like Django standalone - python

I have encountered a hodgepodge of errors trying to bring my django site into production with Apache. Having finally gotten mod_wsgi sorted and Apache at least seeming to be trying to load the site I'm seeing a number of confusing problems in the debug details when the homepage fails to load.
None of these errors emerge when I fire up the site using python manage.py runserver 10.10.10.214:8080 - it works 100% fine that way
Import errors - I cannot from x import y - for some reason it will only allow me to do from x import * An example was #from django.conf.urls import patterns, include, url gave an error - I had to change to from django.conf.urls.defaults import *
Permission errors - for example, I had to chmod the DB file which worked fine before. No big deal I suppose but I just wondered why
DB field errors: Cannot resolve keyword 'user_id' into field. Choices are: end_date, id, singleDate, start_date, user user_id worked just fine in standalone mode.
Apache log was giving errors like TemplateSyntaxError: Caught ImportError while rendering: No module named staticfiles which I just lazily bypassed by commenting out django.contrib.staticfiles in settings.py. This solved nothing, needless to say
It almost felt like the files were being handled on a standalone basis and not within the django framework. Has anyone experienced these kind of teething problems before? I was foolishly thinking I could just plug in. I have a (unfounded) optimistic feeling it must just be a directive in some file.
Cheers, Arthur

Are you sure the target system has the same version of Django installed?
Is the mod_wsgi even compiled for the version of Python you want to use? It is a common mistake that people don't realise that mod_wsgi is actually for a different Python version and so not using the same Python installation as you expect and therefore using different versions of packages.
As to the permissions errors, this is because the code when run under Apache is running as the Apache user by default. It will not have access rights the same as you do when run manually. You also have to be careful on making assumptions about what the working directory of the process is.
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory
In order to try and sort out things out, you may be better to try and use mod_wsgi-express in your development environment. This way development environment is closer to your target system.
http://blog.dscpl.com.au/2015/04/integrating-modwsgi-express-as-django.html
http://blog.dscpl.com.au/2015/04/using-modwsgi-express-with-django.html
http://blog.dscpl.com.au/2015/05/using-modwsgi-express-as-development.html

Related

Host Django application in the Lightsail's built in Apache server

I want to have a production ready Django app with Lighsail and for that I'm following two tutorials to achieve this
Deploy Django-based application onto Amazon Lightsail
Deploy A Django Project
From the Bitnami article can see that the AWS documentation follows its Approach B: Self-Contained Bitnami Installations.
According to:
AWS's documentation, my blocker appears in 5. Host the application using Apache, step g.
Bitnami's documentation, where it says
On Linux, you can run the application with mod_wsgi in daemon mode.
Add the following code in
/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf:
The blocker relates to the code I'm being asked to add, in particular the final part that has
Alias /tutorial/static "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static"
WSGIScriptAlias /tutorial '/opt/bitnami/apps/django/django_projects/tutorial/tutorial/wsgi.py'
More specifically, /home/bitnami/apps/django/. In /home/bitnami/ can only see the following folders
. bitnami_application_password
. bitnami_credentials
. htdocs
. stack
and from them the one that most likely resembles /opt/bitnami/apps/ is /home/bitnami/stack/. Thing is, inside of that particular folder, there's no django folder - at least as far as I can tell (already checked inside some of its folders, like the python one).
The workaround for me at this particular stage is to move to a different approach, Approach A: Bitnami Installations Using System Packages (which I've done and managed to make it work as wrote in this blog post), but I'd like to get it to work using Approach B and hence this question.
The problem here is in the mentioning of the paths for both the project and Django.
In my case, projects are under /home/bitnami/projects/ where I created a Django project named tutorial.
Also, if I run the command
python -c "
import sys
sys.path = sys.path[1:]
import django
print(django.__path__)"
it'll print me the location where Django is installed
['/opt/bitnami/python/lib/python3.8/site-packages/django']
So, the httpd-app.conf should have instead at the end
Alias /tutorial/static "/opt/bitnami/python/lib/python3.8/site-packages/django/contrib/admin/static"
WSGIScriptAlias /tutorial '/home/bitnami/projects/tutorial/tutorial/wsgi.py'

Django Admin Interface - Privileges On Development Server

I have an old project running (Django 1.6.5, Python 2.7) live for several years. I have to make some changes and have set up a working development environment with all the right django and python requirements (packages, versions, etc.)
Everything is running fine, except when I am trying to make changes inside the admin panel. I can log on fine and looking at the database (sqlite3) I see my user has superuser privileges. However django says "You have no permissions to change anything" and thus not even displaying any of the models registered for the admin interface.
I am using the same database that is running on the live server. There I have no issues at all (Live server also running in development mode with DEBUG=True has no issues) -> I can only see the history (My Change Log) - Nothing else
I have also created a new superuser - but same problem here.
I'd appreciate any pointers (Maybe how to debug this?)
Finally, I found the issue:
admin.autodiscover()
was commented out in the project's urls.py for some reason. (I may have done that trying to get the project to work in a more recent version of django) - So admin.site.register was never called and the app_dict never filled. index.html template of django.contrib.admin then returns
You don't have permission to edit anything.
or it's equivalent translation (which I find confusing, given that the permissions are correct, only no models were added to the admin dictionary.
I hope this may help anyone running into a similar problem

In pycharm can I run every file for django?

I'm new to Django. My localhost site is running fine. Since I am using pycharm it is easy to run any file. I decided to run each file in my django project, and came across several errors, such as this one in my views.py:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Even though the site is running, what seems like properly, I'm seeing this message. What is causing this, and is it typical?
You cannot run each file present in your django project individual.
No matter those are file with .py extension. They depend on the django framework to get the project running.
The reason you might be seeing that error is because you might be using the attributes present in the settings.py file which in turn requires django to set the application running like starting the WSGI server, getting all the dependencies and the installed apps ready before you actually use anything.
Understand that Django is a Framework and it relies on many underlying components to actually work. Even thought you can technically run any file in any manner, you cannot start the application itself.
There are other ways to do it if you like to test the application like using django shell by python manage.py shell to check and test the application, which would be a better way of doing individual testing rather than running each file standalone.
You can run any individual python file in a Django Project with Django, but keep in mind that the settings for Django must be supplied. This is not a good practise to run individual file with Django but for debugging purposes you may use it (for example. to test a parser that you wrote with database access),
You have to configure the Django settings before you can do anything with Django on a single file.
from django.conf import settings
settings.configure()
def do_something_with_a_model():
# does something with a model
print "I am done!!"
Note that relative imports may break when running on single files.
(for example. imports like from .another_module import AnotherClass may break when working with single file.)

How do I deploy Django 1.6, Python 3.3, and Apache?

Firstly, the OS I am working in is Fedora 20.
It seems like every way that is mentioned using Django and Apache is either deprecated, or there is no documentation at all.
I have tried mod_wsgi, and I have it all installed, but there is conflicting documentation. One says you should have a django.wsgi (old Django has this), and more recent documentation say you should have wsgi.py. I have tried both methods and all sorts of different things in the httpd.conf file, nothing works, and it all comes back with Forbidden. Also when just trying the Hello World script it always return an internal server error.
I then tried uwsgi. I can use this to get a uwsgi server up, and this seems to basically work in a similar fashion to just using python3 manage.py runserver, so I installed mod_proxy_uwsgi module for Apache, and there is absolutely no documentation on using this, so I just did something like ProxyPass / wsgi//localhost:8000/ in the httpd.conf. All this does is also returning an internal server error.
The main answer I want is: How exactly do I deploy a Django 1.6 project that uses Python 3, onto Apache?
This didn't work for you? It is listed under Django 1.6... then again, Python 3.x is still kind of iffy for a lot of stuff.
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
This has been resolved. As it turns out, SELinux was blocking httpd from accessing my Django project. I just used the following command then restarted the computer:
setsebool -P httpd_read_user_content 1
setsebool -P httpd_enable_homedirs 1
This will allow MOD_WSGI to work properly for deployment.

Uploading Django Development to Dreamhost/Passenger first time "Could no import"

I've been developing on my laptop.
Now, I've uploaded my development project from Django 1.2/Python 2.7 up to Dreamhost created using the Passenger setup. I am using South for migration.
I modified settings.py to access MySQL. Got Admin working.
Questions:
1) Where is the std out shown when you hit the site? (all my print statements)
2) I had to add
from decimal import Decimal
to get the settings.py to run.
Now, hitting the server gives me a 500. When I go to the commandline and try any manage.py command I'm getting:
Error: Could not import settings 'pholdershare.settings' (Is it on sys.path? Does it have syntax errors?): No module named settings
I modified the permissions for my folder to 755 but that didn't change anything.
3) Regarding development to testing to live, I'm totally unclear on how to do this. I want to have a nice solid system for development where I can do releases but I've never done that. I have been using GIT locally. Can anybody point me to the tutorial on setting up the release system?
Suggestions?
Thanks much!
"Could not import settings 'pholdershare.settings'"
...usually means either settings.py has an import error or syntax error, or pholdershare has not been added to sys.path in your wsgi.py

Categories