Django custom auth backend not recognized on Apache - python

I'm trying to deploy my Django application to an Apache2 based server with mod_python. I've set the handlers right and made the configuration to make mod_python work with my project. My project implements a custom auth backend to connect my users to twitter, and my backend implementation is on:
myproject
|- backends/
directory.Everything seems to be working fine, my pages load and I can make read/write operations properly. But whenever I try to login with my Twitter account, application fires an exception telling me:
Error importing authentication backend backends.twitteroauth: "No module named backends.twitteroauth"
In my settings.py, I'm registering my backend as
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'myproject.backends.twitteroauth.TwitterBackend',
)
What is the problem?

Removing database solved my problem. As far as I can guess, if a user is logged, his corresponding login backend is kept as a session variable on the database. My settings.py file was
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'backends.twitteroauth.TwitterBackend',
)
before I made the correction. Changing settings.py and restarting the application was simply not enough. You have to remove session related records from db too.

The problem is that python cannot find the module twitteroauth. What is the name of the file TwitterBackend is in? Also make sure that there is a __init__.py file in backends to mark it as a package.
edit:
What happens if you run the shell
python manage.py shell
and try to import it there?
from myproject.backends.twitteroauth import TwitterBackend
As anything else works fine, I guess myproject is in your python path.

Make sure that backends is on the python path and has a init.py file in the folder.

Related

Django API keeps loading and throws timeout error when 'statsmodels.api' package is imported

In the urls.py
from django.contrib import admin
from django.urls import path
import pandas as pd
import statsmodels.api as sm
urlpatterns = [path('admin/',admin.site.urls),]
When I comment out the 'import statsmodels.api as sm' line, the API is working fine and shows the django home page but when I include the statsmodels package it keeps on loading and throws timeout error. Please suggest what is going wrong. The packages are installed properly in the django environment.
Additional information: (Python version - 3.8.5, Django - 3.1.4, Ubuntu - 20.4)
Deployed this django API (installed apache2 and mod_wsgi with this django) in the Ubuntu EC2 instance and calling the django API from the local computer using
Public IPv4 DNS. (Followed this site to deploy the django and apache - https://studygyaan.com/django/how-to-setup-django-applications-with-apache-and-mod-wsgi-on-ubuntu)
Solved the problem finally (Check Out:Django Webfaction 'Timeout when reading response headers from daemon process')
Python C extension modules, like statsmodels/numpy, are known to cause timeouts when used under mod_wsgi. There's a clear explanation of the problem (direct from the author of mod_wsgi) available at https://serverfault.com/a/514251/109598
If that sounds like it might be the cause of your problem, then the solution is probably simple - add the following to your apache2.conf. Go to the apache2.conf in the etc/apache2/apache2.conf and add the below code at the end
WSGIApplicationGroup %{GLOBAL}
Be sure to restart your Apache instance after making that change
The urls.py file is not a place for using statsmodels. You can use statsmodels in views.py, then define URL for that view in urls.py file as well.

Unable to fix error DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

Please help resolving this env issue/error. I did found many posts about resolving that error but I still couldn't fix mine. I guess I am just unable to understand it in the right way.
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I have just one app in my Django project, had created using django-admin startproject. The app is added to settings.py under INSTALLED_APPS, and all other settings are set to default by Django itself which i haven't modified.
I have few def in views.py working on models/forms, and those are working well.
I also have created a new folder (myscripts) in the project (next to the app-name folder), and in that myscripts folder i have my other py scripts (other than models/forms). Executing those scripts works, I am able to import those models/forms, it does some work and adds a new entry in django db sqlite3 correctly.
Further, i want to add/update execution status to the db entry (for a given pk/id) after my other scripts finishes its work.
To achieve that, I am trying a new py script which will import models to get its objects so i can update contents in the db but that fails. (throws me above error).
Based on other posts, i did try adding env variable DJANGO_SETTINGS_MODULE=my-app-name.settings, it doesn't work (i just have one app and django has it already configured -- i can see that in wsgi.py as os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-app-name.settings') ).
I also tried code from django.conf import settings , but that also with no success.
However, when I run "python manage.py shell" and import models and its objects they don't throw error but thorws error when i run the update py script.
Please guide me where i can look for to fix the issue.

How to hide EMAIL_HOST_PASSWORD in settings.py file?

So, I have an application written with Django and it has a contact page, from where users can send mail using gmail's smtp. For this functionality in settings.py file I wrote EMAIL_HOST_PASSWORD = 'my-own-password', and I'm gonna deploy my site on github.io. So, I must hide or encrypt password. What can I do with that ?
Two things about your question:
You cannot deploy Django app on github pages because they serve only static files. Check e.g. Heroku, it's quite easy to deploy Django there.
The most common practice to hide passwords and secret keys is to use environmental variables. Generally, you have to set a variable in bash e.g. export EMAIL_HOST_PASSWORD=my-own-password and then you can use os python module to retrieve it:
import os
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD")
Useful video tutorials:
Python Django Tutorial: Deploying Your Application (Option #2) - Deploy using Heroku
Python Quick Tip: Hiding Passwords and Secret Keys in Environment Variables (Mac & Linux)
You can't deploy a django application on github pages as described here : https://help.github.com/en/articles/what-is-github-pages.
If you want to upload you code on github and you don't want to share your password you can put it in another file and add this file to your .gitignore

Django: Push app from local server to production server via FTP

This is a bit embarassing, but I'm a Django noob and I couldn't find a simple solution to this:
I have written a Django app in a local VM that I now want to deploy to a "production" server. App works like a charm locally.
Now my IT colleague has set up the server with Django and that also works fine. I can open it via the Web and I get the usual "Congratulations on your first Django-powered page". I can also log into the admin interface. The project has been created.
This is a very low-key mini project and I'm not too familiar with git, so we've decided to just push files via FTP. (And I want to stick with that if at all possible.) So I uploaded the app folder into the project folder and also adjusted the project's settings.py and urls.py.
However, nothing seems to be happening on the server's end. The welcome page is the same, the app does not show up in the admin interface and the URLs won't be resolved as hoped.
Any suggestions what I should have done / done differently?
You need to restart apache or whatever is running your django project. Your changes to py files are cached when you first load your server config (settings).
Any suggestions what I should have done / done differently?
You should be using git/jenkins/deployment techniques, I know you said you've decided not to use it but you're going to be missing out on important things like being able to keep track of changes and unit testing

Django settings outside of project

I have a Django project that uses SQLAlchemy to use some legacy ORM objects. This application also hits up an ldap server for user authentication. I was getting sick of moving from development to production servers for both ldap and the database. I was hoping to create a IS_DEVELOPMENT variable in the settings.py. One problem I am running into is that the ORM modules are not in the Django project.
I know django.conf does a great job of parsing that settings file and making those settings available to you throughout your application. What I can not figure out is how to make those same settings available to myself outside of that app.
Anyone done this one before?
"Standalone Django Scripts"

Categories

Resources