I made a new app for my webapp project using ./manage.py startapp webapplogin.
The templates all works however now I keep getting 'WSGIRequest' object has no attribute 'session' whenever I try to do anything with request such as logout(request) or login(request, user).
I don't understand what the issue is after about 6 hours of debugging/googling...
Do I need to go back to doing the webapp all in webapp instead of trying to split up the project into apps? The templating works fine as well as linking right now, just the no attribute 'session'...
project structure below
app_project
app_backend
settings.py
urls.py
wsgi.py
static
static_files
webapp
templates
webapp
base.html
home.html
admin.py
apps.py
models.py
tests.py
urls.py
views.py
webapplogin
templates
login
login.html
admin.py
apps.py
models.py
tests.py
urls.py
views.py
project settings below
INSTALLED_APPS = [
'webapplogin.apps.LoginConfig',
'webapp.apps.WebappConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.humanize',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'app_backend.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'app_backend.wsgi.application'
currently on 1.9, Django doesn't recognize the MIDDLEWARE setting. You should use the MIDDLEWARE_CLASSES setting
The reason this happens is because the order of the middleware is important.
During the request the Django session middleware must execute first in order to add the session object to the request.
Try the following :
MIDDLEWARE = [
'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',
'django.middleware.security.SecurityMiddleware',
]
Related
Well I am trying to add login and sign up with facebook functionality and when i click on login with facebook it takes me to facebook but shows an error such as
Facebook has detected Believer isn't using a secure connection to transfer information.
Until Believer updates its security settings, you won't be able to use Facebook to log into it.
I used comments for those lines which i add for social app. This code is from my project/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'accounts',
'social_django', # <--adding social app
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.github.GithubOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
'social_django.middleware.SocialAuthExceptionMiddleware', #<--for social app
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends', # <-- Here
'social_django.context_processors.login_redirect', # <-- Here
],
},
},
]
LOGIN_REDIRECT_URL = 'test'
LOGOUT_REDIRECT_URL = 'thanks'
SOCIAL_AUTH_FACEBOOK_KEY = '1289678764728890' # App ID
SOCIAL_AUTH_FACEBOOK_SECRET = '1c2a66c9c8116e4e2d97cb059db5d6a9' # App Secret
I need to get some information from LinkedIn profile of a user to fill existing fields of my application form. So I am using LinkedIn API(I have created app in linkedin and stand with client_Key, client_Secret,r_liteprofile and r_emailaddress permissions). Now it is prompting me to sign-in with linkedIn and asking for "Allow" permission(All OK till this prompt) but In the next page I'm getting below error instead of showing json file with user information.
"""
HTTPError at /complete/linkedin-oauth2/
410 Client Error: Gone for url: https://api.linkedin.com/v1/people/~:(first-name,id,last-name)?oauth2_access_token=AQVpVZCnhj2oLkzUvQytDU89kJUMm5yEIadV0BZMid3WVqPSVdOiJIePGIH7ZL7i3M5gppOlbUqpS68rDaaio56Y-nkC3Njpvf91v8WUPxQ8t-3uqJRzCC_MdrKUpntLalp24Eo2BMjpYIKeHGdxjFIlaMt9tszkIVHpPcZA2-dgbqOBrvt9-QE4P91bTXqBBkrtHEXg9F560OvltnQDgc0U1xwO-5yOT5LjlqAtvDJ_gMf3G8rZ9cdkayq4aP1CO-ljglGqlJb4uxorPRg7qPqqkNaAmQjXploM0KVQ6pK7nidP4zC2l7WW1aqg38GRDe8AM8jzaiIkg-SX3JfCJA28fT9H8A&format=json
"""
Below is my code, Did I miss anything....I'm newbie to Django Please help me to fix it??
I have tried to get exact access token format like{access token:XXXXX,expires in:XXX} with url https://www.linkedin.com/oauth/v2/accessToken?grant_type=client_credentials&client_id=[my_client_ID]&client_secret=[my_client_secret] I got below response.
{"error":"access_denied","error_description":"This application is not allowed to create application tokens"}
How can I achieve it??
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'bootstrap4',
'social_django',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
AUTHENTICATION_BACKENDS = (
'social.backends.linkedin.LinkedinOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_LINKEDIN_OAUTH2_KEY = 'my_client_ID'
SOCIAL_AUTH_LINKEDIN_OAUTH2_SECRET = 'my_client_secret'
SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_liteprofile']
SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = ['r_emailaddress']
SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA = [('id', 'id'),
('firstName', 'first_name'),
('lastName', 'last_name'),
('emailAddress', 'email_address')]
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/home/'
SOCIAL_AUTH_LOGIN_URL = '/'
```[the error I'm getting][1]
[1]: https://i.stack.imgur.com/ECS3N.png
So i'm trying to build a basic blog in Django (am currently using the latest version of Django) and i'm running into a really annoying problem. When I try and set up my html templates, I keep on getting a templatedoesnotexisterror.
Here's the rub--if I set up my html templates in the root app of the project ("blogcode"), they run perfectly. But then, once I start running another app ("articles") and then I set up a templates folder using articles/templates/articles/homepage.html, all of the sudden, it doesn't work. I can't get django to look anywhere but in the root app directory to find and ultimately render templates.
In my settings.py file, i've the DIRS list set to 'templates'. When I tried changing it to os.path.join(BASE_DIR, 'templates') I get the same "templatedoesnot exist" error. Also, my app IS properly installed in the INSTALLED_APPS list in settings.
I've tried looking in other documentation, but the only hints I can find are really outdated. On the views.py, if I chop off the articles/, and just leave it as 'homepage.html' django renders it from the root app just fine and ignores the template in the articles app, but if I try and get it to render from the articles app, I get the "templatedoesnotexist" error. What's going on?
Here's my code:
articles/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.startpage, name='startpage'),
]
articles/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Article
def startpage(request):
return render(request, 'articles/homepage.html')
blogcode/settings.py
INSTALLED_APPS = [
'articles.apps.ArticlesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'horror_blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'horror_blog.wsgi.application'
Try register the app that way, and put django apps above your owns
Instead of
'articles.apps.ArticlesConfig',
Try this:
INSTALLED_APPS = [
''' django apps'''
'articles'
]
this 'DIRS': ['templates'] should be 'DIRS': [os.path.join(BASE_DIR, 'templates'),],
I have django app that is using postgresql as a backend. It have different authentication method and this app running on server A .
setting.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login',
'reports',
)
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.security.SecurityMiddleware',
)
ROOT_URLCONF = 'reporting.urls'
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_PATH],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'reporting.wsgi.application'
and my second application is on Nodejs with backend mongoDB and authentication method is passport-local. It is running on server B
primary app is Django.
Now I want If Login from django app and click to link that directly go to Nodejs app without login.
Or any other method for login in both application with single authentication.
I'm trying to create a data visualization for yahoo finances, which I've successfully done locally using Django 1.9.2. It scrapes yahoo finances and then uses D3 to create bubble charts with tooltips.
However, the bubbles don't show up:
https://pure-woodland-72284.herokuapp.com/
This is what appears in Heroku logs.
Heroku Log Error Messages
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_swagger',
'mysite'
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
os.path.join(os.path.dirname(__file__), 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
REST_FRAMEWORK={
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
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.security.SecurityMiddleware',
)
Your console have the following errors:
Mixed Content: The page at 'https://pure-woodland-72284.herokuapp.com/' was loaded over HTTPS, but requested an insecure stylesheet 'http://fonts.googleapis.com/css?family=Lobster+Two:400,700italic'. This request has been blocked; the content must be served over HTTPS.
Hence you are getting 'Uncaught ReferenceError: $ is not defined' error since you did not really imported your libraries
So I'm guessing that you have imported your scripts (jquery,d3.tip,cssfonts) from their cdn and used http while importing. Try importing them with https and see if that solves your problem