django nonrel ImportError when running a script - python

I've configurated a virtual environment for my project, all is working except that when I try to run a script to populate the MongoDB data base. I get a ImportError exception.
I am using django mongodb engine Link and djangotoolbox. I want to run a script to populate my mongo db, however when I run it the following exceptions raised:
This is my project skeleton:
This is my models.py file:
from django.db import models
from djangotoolbox.fields import EmbeddedModelField, ListField
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
pages = ListField(EmbeddedModelField("Page"))
def __unicode__(self):
return self.name
class Page(models.Model):
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __unicode__(self):
return self.titleenter code here
This is my settings.py file:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from pip.exceptions import BadCommand
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'mw7+iswll+f=a!xg9r+*%v=uyv$r_6lqcf)eb27s!4h_!*&9gt'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rango',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'tango_with_django_project.urls'
WSGI_APPLICATION = 'tango_with_django_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'tango_with_dj',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
MEDIA_URL = '/media/'
MEDIA_ROOT
This is my populate script populate.py:
import os
def populate():
python_cat = add_cat('Python')
django_cat = add_cat("Django")
frame_cat = add_cat("Other Frameworks")
# Print out what we have added to the user.
for c in Category.objects.all():
print "Category: "+c.name+" was added!"
def add_cat(name):
c = Category.objects.get_or_create(name=name)[0]
return c
# Start execution here!
if __name__ == '__main__':
print "Starting Rango population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django_project.settings')
from rango.models import Category
populate()
I don't know why djangotoolbox cannot be reached at the populate main scope if it is imported at models.py scope. What is the problem here?

Related

Issue with django staticfiles amazon S3

i just having this issue, and I wonder if you can help me, I'm using django 1.7, my project runs great on local, I make a deploy in EC2 of amazon, with nginx, gunicorn, etc, the project runs well in the instance of EC2, but when I config the settings in the production.py, the styles, images upload to the bucket in S3, but doenst load in the project, in the main page.
Im using pillow for the administration of the images and static..
I just dont know why it doesnt work, because the static files are in the bucket. Please help me, here I put the codes of my base.py, production.py.
I used python manage.py collectfiles and , it collects, but it doesnt load the styles when I open the project in the web.
BASE.PY
from unipath import Path
BASE_DIR = Path(__file__).ancestor(3)
SECRET_KEY = '<my secret key>'
DJANGO_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
LOCAL_APPS = (
'apps.eventos',
'apps.users',
)
THIRD_PARTY_APPS = (
'social.apps.django_app.default',
)
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'ueventos.urls'
WSGI_APPLICATION = 'ueventos.wsgi.application'
LANGUAGE_CODE = 'es'
TIME_ZONE = 'UTC'
DATE_FORMAT = "Y-m-d"
USE_I18N = True
USE_L10N = False
USE_TZ = True
AUTH_USER_MODEL = 'users.User'
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookAppOAuth2',
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOpenId',
'social.backends.google.GoogleOAuth2',
'social.backends.google.GoogleOAuth',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_USER_MODEL = 'users.User'
SOCIAL_AUTH_FACEBOOK_KEY = '776670565804594'
SOCIAL_AUTH_FACEBOOK_SECRET = 'fcc2a240ef71ff88c15fd64d685bf4eb'
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '435717486239-b9dpg2pfian4h8dj1vc95jna8tfdeced.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'oE9ZzK_p9zuNxH91gRLS8nH1'
PRODUCTION.PY
from .base import *
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<mydb>',
'USER': '<mydbuser>',
'PASSWORD' : 'mydbuser',
'HOST': 'localhost',
'PORT': '5432'
}
}
INSTALLED_APPS = INSTALLED_APPS + (
'storages',
)
AWS_STORAGE_BUCKET_NAME = 'ueventos'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorages'
AWS_ACCESS_KEY_ID = 'mykey'
AWS_SECRET_ACCESS_KEY = 'mysecretkey'
STATIC_URL = 'https://s3.amazonaws.com/ueventos/'
MEDIA_URL = 'https://s3.amazonaws.com/ueventos/'
MEDIA_ROOT = BASE_DIR.child('media')
I already install boto and django storages.. eveything works fine on the server, the issue is with the static in s3.
in my manage.py and wsgi.py I specify to use de production.py file.
I have a folder named "media", where all the files the people upload, will save there. But that folder doenst apper in s3. I tried uploading some image in my site, but doesnt work anyways.
I'm using 2 apps, one for events and another for user.. both of them storage images..
Thank you so much.
Try something like this:
from storages.backends.s3boto import S3BotoStorage
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
DEFAULT_FILE_STORAGE = 'config.settings.production.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'config.settings.production.StaticRootS3BotoStorage'
MEDIA_URL = 'https://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = 'https://s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME
Adjust path accordingly.
Also see:How to set-up a Django project with django-storages and Amazon S3, but with different folders for static files and media files?

"No module named context_processors" django

I am getting No module named context_processors using django 1.7 due to django-baker requirements
main urls:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('account.urls')),
url(r'^postings/', include('postings.urls')),
)
settings.py:
"""
Django settings for myproject project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'byq8q##!vc+!-(my3s0g)_-y4lyhvv#4+u2f-r_)v04m65ogq!'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'django_baker',
'bootstrapform',
# 'emailusernames',
'pinax_theme_bootstrap_account',
'django_forms_bootstrap',
'account',
'postings',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'account.middleware.LocaleMiddleware',
'account.middleware.TimezoneMiddleware',
)
ROOT_URLCONF = 'myproject.urls'
WSGI_APPLICATION = 'myproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
TEMPLATE_DIRS = [os.path.join(BASE_DIR, "templates"),]
TEMPLATE_LOADERS = ('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader')
TEMPLATE_CONTEXT_PROCESSORS = [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'account.context_processors.account',
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
AUTHENTICATION_BACKENDS = (
# 'emailusernames.backends.EmailAuthBackend',
'account.auth_backends.EmailAuthenticationBackend',
# Uncomment the following to make Django tests pass:
# 'django.contrib.auth.backends.ModelBackend',
)
LOGIN_URL = "/accounts/login"
ACCOUNT_EMAIL_UNIQUE = True
ACCOUNT_EMAIL_CONFIRMATION_REQUIRED = True
THEME_ACCOUNT_CONTACT_EMAIL = "cchilder#mail.usf.edu"
I followed the instructions at http://django-user-accounts.readthedocs.org/en/latest/installation.html
and http://django-user-accounts.readthedocs.org/en/latest/installation.html
I see the correctly named templates in the https://github.com/pinax/pinax-theme-bootstrap-account/tree/master/pinax_theme_bootstrap_account/templates/account docs, but this error occurs when I try http://127.0.0.1:8005/account/signup/
How can I make these pinax themes work for django 1.7? Thank you

Django Looking for URLS of a Different Project

I have two projects in django that I´m working on at the same time. Today something really strange happened after switching projects.
I have my first project: urls.py and manage.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'agenda.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^agenda/', include('modulo_agenda.urls')),
url(r'^schedule/', include('schedule.urls')),
)
"""
Django settings for agenda project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__), ".."),
)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
TEMPLATE_PATH,
)
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#7j23xm3jv=(#gicejabv2ppa$063st+d#)2x^thld0(#!chwq'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'modulo_agenda',
'schedule',
'djangobower',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
)
ROOT_URLCONF = 'agenda.urls'
WSGI_APPLICATION = 'agenda.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
MEDIA_ROOT = os.path.join(PROJECT_DIR, "site_media")
ADMIN_MEDIA_PREFIX = '/media/'
MEDIA_URL = '/site_media/'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.request",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
)
BOWER_PATH = 'C:/Python34/Lib/site-packages'
BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_ROOT, "components")
BOWER_INSTALLED_APPS = (
'jquery',
'bootstrap'
)
FIRST_DAY_OF_WEEK = 1 # Monday
ROOT_URLCONF = 'agenda.urls'
And my second project settings and urls:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'proyecto_final_web.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^music/', include('music_manager.urls')),
)
"""
Django settings for proyecto_final_web project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=%65sc*)pue#)wj&pxd#meh3s_v(^s***+ns(p*8_#pjla_+xo'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'proyecto_final_web.urls'
WSGI_APPLICATION = 'proyecto_final_web.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_PATH=os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (TEMPLATE_PATH,)
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
The problem I´m having is that when I type run server I get on my agenda project I get:
System check identified no issues (0 silenced).
August 30, 2015 - 13:33:04
Django version 1.7, using settings 'agenda.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
It´s saying it is using agenda.settings, which has ROOT_URLCONF = 'agenda.urls'
But when trying to go to the URL localhots:8000/schedule I get this error:
Using the URLconf defined in proyecto_final_web.urls, Django tried these URL patterns, in this order:
^admin/
^music/
The current URL, schedule/, didn't match any of these.
Which are the URLS defined in my other project. I don´t understand why this happens since on the runserver it is saying it´s using the agenda.settings and not the proyecto_final_web.settings.
Can anyone tell me what might be happening?
Judging from the code above, there seems to be error in PROJECT_ROOT settings. One project has PROJECT_ROOT setting defined and other project does not seem to have this setting.
You need to ensure that PROJECT_ROOT has been defined correctly and for both the projects.

AttributeError: 'module' object has no attribute 'META' in Django Project

I am working on a blog in Python-Django. When I run the command "python manage.py runserver" in my project folder, I get following error:
Traceback (most recent call last):
File "manage.py", line 6, in <module>
server = request.META.get('wsgi.file_wrapper', None)
AttributeError: 'module' object has no attribute 'META'
I have not seen this error anywhere. Should I include Meta class in my installed_apps? How do I do it?
Here is my manage.py:
#!/usr/bin/env python
import os
import sys
from django.http import request
server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
print('inside dev')
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dj1.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
and settings.py:
"""
Django settings for dj1 project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(l8*%h2ids25q=x9qb7%x+(b=$1mm&yg8b5ga^0u2w9*+k%+9-'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'dj1.urls'
WSGI_APPLICATION = 'dj1.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
This is really a big code bulder.
This piece of code here makes no sense inside the manage.py
from django.http import request
server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
print('inside dev')
Perhaps you copied it from somewhere, and didn't put it where it belongs.
This piece of coude would belong in a view. It will never work inside manage.py, and makes asolutely no sense there.
[EDIT]
ALSO, in a view, you should NOT include the import, or you'll get the same error. That means, don't include this from django.http import request

Django AUTHENTICATION_BACKENDS import error

What is the proper way to import a custom backend in settings.py? I currently have the following in settings.py:
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
where apployment_site is the app, auth is file name, and CustomAuth is the class name.
In my view, I get: ImportError: a doesn't look like a module path after I run the following code:
from django.contrib.auth import authenticate
from apployment_site import *
authenticate(username="username", password="password")
Here's my full settings.py:
"""Django settings for apployment project.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3(a=jr=tfedkqzv3f=495%0$ygxjt332(=n0&h=e2bzh(i#r*j'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apployment_site'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
ROOT_URLCONF = 'apployment.urls'
WSGI_APPLICATION = 'apployment.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
make sure it's a tuple:
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth',)
note the comma at the end
I don't think you need to actually import it into your view. According to the documentation, Django will go through all your authentication backends when you call authenticate().
Thus, just make sure you have all the authentication backends you want in your settings.py.

Categories