I am trying to use graphQL queries in django. Basically I have two apps, my 'api' app which contains everything I need to make the queries and another one called 'frontend' from which I call the api to use these queries.
I can use the GraphQL view to type queries in it and it works perfectly, but whenever I try to make the query, I get this: "OrderedDict([('users', None)])"
Result of my query in the GraphQl view
The code:
In 'api' my schema.py:
import graphene
import graphql_jwt
from graphene import relay, ObjectType, AbstractType, List, String, Field,InputObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from datetime import date, datetime
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
....
class Query(graphene.ObjectType):
me = graphene.Field(UserType)
users = graphene.List(UserType)
profile = relay.Node.Field(ProfileNode)
all_profiles = DjangoFilterConnectionField(ProfileNode)
def resolve_users(self, info):
### Returns all users ###
user = info.context.user
if user.is_anonymous:
raise Exception('Not logged!')
if not user.is_superuser:
raise Exception('premission denied')
return User.objects.all()
def resolve_me(self, info):
### Returns logged user ###
user = info.context.user
if user.is_anonymous:
raise Exception('Not logged!')
return user
def resolve_all_profiles(self, info, **kwargs):
### Returns all profiles ###
return Profile.objects.all()
.....
def execute(my_query):
schema = graphene.Schema(query=Query)
return schema.execute(my_query)
And the views.py that calls the app 'api' in my app frontend:
from django.shortcuts import render
import graphene
from api import schema
from django.contrib.auth import authenticate
def accueil(request):
if request.user.is_authenticated:
check = "I am logged"
else:
check = "I am not logged"
result = schema.execute("""query {
users {
id
username
}
}""")
return render(request, 'frontend/accueil.html', {'result' : result.data, 'check' : check})
The template :
<h1>OTC</h1>
<p> the users are : {{result}}</p>
<br/>
<p>{{check}}</p>
login
logout
and finally:
The web page result
and the error in the console:
An error occurred while resolving field Query.users
Traceback (most recent call last):
File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executor.py", line 311, in resolve_or_error
return executor.execute(resolve_fn, source, info, **args)
File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executors/sync.py", line 7, in execute
return fn(*args, **kwargs)
File "/home/victor/poc2/poc2/api/schema.py", line 67, in resolve_users
user = info.context.user
AttributeError: 'NoneType' object has no attribute 'user'
Traceback (most recent call last):
File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executor.py", line 330, in complete_value_catching_error
exe_context, return_type, field_asts, info, result)
File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executor.py", line 383, in complete_value
raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'user'
Unless you're writing a test client, you probably should not be calling schema.execute from inside a Django view. But assuming that you have your reasons for doing this, your specific problem is that you're not passing the user when you invoke schema.execute in accueil view.
Have a look at the execute documentation and you'll see that you'll need to supply an optional argument for the context. Your code is not supplying a context, hence the info.context is None, as per your exception. Unfortunately, the example
result = schema.execute('{ name }', context_value={'name': 'Syrus'})
is not Django-specific. But I think what works in a Django functional view is:
result = schema.execute(query, context_value=request)
Related
I am getting the following error trying to retrieve a user profile from the mongodb:
New searches for user 555555555
[{'search_term': 'ibm'}, {'search_term': 'ry'}]
Internal Server Error: /users/555555555
Traceback (most recent call last):
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/cchilders/projects/stocks_backend/users/views.py", line 50, in get_user_profile
user = UserProfile.objects.get(user_id=user_id)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/db/models/query.py", line 433, in get
raise self.model.MultipleObjectsReturned(
users.models.UserProfile.MultipleObjectsReturned: get() returned more than one UserProfile -- it returned 2!
[21/Aug/2022 09:45:31] "POST /users/555555555 HTTP/1.1" 500 76920
Having multiple user profiles is application breaking and there should only be one for each user. I am also getting duplicates in another model StockInfo.
my view appears to only save a new user when none exists for that user id so I'm confused
users/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json
from helpers.view_functions import parse_request_body
from .models import UserProfile
#csrf_exempt
def get_user_profile(request, user_id):
if request.method == 'GET':
try:
user = UserProfile.objects.get(user_id=user_id)
print("user found")
except UserProfile.DoesNotExist:
print("user does not exist exception")
profile = UserProfile()
profile.user_id = user_id
profile.searches = [
{'search_term': 'hd'},
{'search_term': 'wba'},
]
profile.display_settings = [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
profile.save()
print("user saved in db")
user = UserProfile.objects.get(user_id=user_id)
except Exception as error:
print("got an unknown exception:")
print(error)
data = {
'user_id': user.user_id,
'searches': user.searches,
'display_settings': user.display_settings
}
json_data = json.dumps(data)
return HttpResponse({json_data}, content_type='application/json')
if request.method == 'POST':
body = parse_request_body(request)
searches = body['searches']
searches_objects = [{'search_term': x} for x in searches]
print("New searches for user {user_id}".format(user_id=user_id))
print(searches_objects)
user = UserProfile.objects.get(user_id=user_id)
user.searches = searches_objects
user.display_settings = body['display_settings']
user.save()
return HttpResponse("it worked")
in case it matters here's users/models.py:
from djongo import models
class RecentSearch(models.Model):
search_term = models.CharField(max_length=100)
class Meta:
abstract = True
class DisplaySetting(models.Model):
setting_name = models.CharField(max_length=150)
visible = models.BooleanField()
class Meta:
abstract = True
class UserProfile(models.Model):
user_id = models.CharField(max_length=255)
searches = models.ArrayField(model_container=RecentSearch, null=True)
display_settings = models.ArrayField(model_container=DisplaySetting, null=True)
objects = models.DjongoManager()
Is user = UserProfile.objects.get(user_id=user_id) asynchronous and not returning in time to be checked in the try statement? I wouldn't expect python to have a race condition here since I'm not using any async keywords.
When I change code to use get_or_create it breaks again, getting error that there are more than 1 user profile:
def get_user_profile(request, user_id):
if request.method == 'GET':
user, created = UserProfile.objects.get_or_create(user_id=user_id, defaults={
'user_id': user_id,
'searches': [
{'search_term': 'hd'},
{'search_term': 'wba'},
],
'display_settings': [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
})
if created:
print("user saved in db")
data = {
'user_id': user.user_id,
'searches': user.searches,
'display_settings': user.display_settings
}
json_data = json.dumps(data)
return HttpResponse({json_data}, content_type='application/json')
I tried passing in user_id in the default dict also.
New searches for user 114260670592402026255
[{'search_term': 'ibm'}, {'search_term': 'ry'}, {'search_term': 'td'}]
Internal Server Error: /users/114260670592402026255
Traceback (most recent call last):
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/cchilders/projects/stocks_backend/users/views.py", line 62, in get_user_profile
user = UserProfile.objects.get(user_id=user_id)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/cchilders/.local/lib/python3.10/site-packages/django/db/models/query.py", line 433, in get
raise self.model.MultipleObjectsReturned(
users.models.UserProfile.MultipleObjectsReturned: get() returned more than one UserProfile -- it returned 2!
[21/Aug/2022 16:23:20] "POST /users/114260670592402026255 HTTP/1.1" 500 76995
Using transaction.atomic also causes duplicate user profiles:
#csrf_exempt
def get_user_profile(request, user_id):
if request.method == 'GET':
# try:
with transaction.atomic():
user, created = UserProfile.objects.get_or_create(user_id=user_id, defaults={
'user_id': user_id,
'searches': [
{'search_term': 'hd'},
{'search_term': 'wba'},
],
'display_settings': [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
})
user = UserProfile.objects.get(user_id=user_id)
data = {
'user_id': user.user_id,
'searches': user.searches,
'display_settings': user.display_settings
}
json_data = json.dumps(data)
return HttpResponse({json_data}, content_type='application/json')
I need this view to never save a duplicate user profile once the first one is made
In your django APPLICATION (not project)
create a file called signals.py
Go to the "apps.py" file, and add the following ready() method to the class.
class MyApplicationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapplication'
def ready(self):
import myapplication.signals
^^^ The class itself is just an example, the ready method is self explanatory
Then we will get to creating a signal -> go into signals.py
from django.dispatch import receiver
from django.db.models.signals import post_save
from myapplication.models import Userprofile
from django.contrib.auth import get_user_model
# ^ Nescessary imports to create a signal to be sent after User creation.
#receiver(post_save, sender=get_user_model())
def create_profile_signal(
sender, # The User class specified above
instance, # The actual user instance that was edited
created, # self explanatory
**kwargs # Nescessary kwargs.
):
if created: # Assign the current user's instance to the userprofile to create one.
profile = UserProfile.objects.create()
profile.user_id = instance.id # -> profile.user = instance if you have a OneToOneField named user.
profile.searches = [
{'search_term': 'hd'},
{'search_term': 'wba'},
]
profile.display_settings = [
{'setting_name': 'showYieldChange', 'visible': True},
{'setting_name': 'showAllDividends', 'visible': True},
]
profile.save()
Then, you need to fully restart your django project (Stop and start the built-in runserver), django is not good at picking up on newly created files.
So what now?
Every time a user signs up to your django project, the userprofile will automatically be created. You do not need to do this yourself. No hassle in views and such. You can ofcourse, also set defaults like before.
Some extra information:
Django includes a “signal dispatcher” which helps decoupled applications get notified when actions occur elsewhere in the framework
There are a lot of signals, it's nice to look into. Examples are pre_save, post_save, post_delete, m2m_changed
Most of them take different arguments.
Whenever a model gets saved, a signal gets sent.
So when the user signs up, a signal gets sent.
In post_save, we can specify the created argument, we use this to check if the user that was saved was created
We have created a function to 'capture' that signal.
In that function we can apply logic to the instance. Please, do not call .save() on the instance in signals. Unless it's in the if created: login, it will create a loop and error.
If you seek to edit a model instance in signals, use pre_save, and don't save the instance afterwards.
What else do I need to do in the view?
You basically don't have to do anything if you implemented the signal. Whenever a user signs up, the profile will get created automatically.
What do I need to do in the model?
You dont /HAVE/ to do anything with the model,
Optional:
but I think this field on the userprofile will help you out a lot: user = models.OneToOneField('auth.User', on_delete=models.CASCADE, null=False, blank=False)
I'm trying to get a list of Scripts
I'm not sure why this error is django throwing however, Can anybody identify if there's something is wrong.
Model
class Script(models.Model):
name = models.CharField(max_length=100)
script = models.TextField()
def __str__(self):
return self.name
Serializer
class ScriptSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(read_only=True)
script = serializers.CharField(read_only=True)
view;
#api_view(['GET'])
def scripts(request):
scripts = Script.objects.all()
serializer = ScriptSerializer(scripts, many=True)
return Response(serializer.data)
but when I call this view, I get this error
Internal Server Error: /api/v1/seo/scripts
Traceback (most recent call last):
File "E:\folder\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "E:\folder\venv\lib\site-packages\django\utils\deprecation.py", line 136, in __call__
response = self.process_response(request, response)
File "E:\folder\venv\lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response
if response.get("X-Frame-Options") is not None:
AttributeError: 'Script' object has no attribute 'get'
I have also seen answers related to that issue in Stackoverflow but not able to reproduce the solution. And also this approach is not new to me, I have been using this type of functions all over the project. But I'm surprised now why it's happening.
Edit:
Imports
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import ScriptSerializer
from .models import Script
urls
from django.urls import path
from . import views as views
app_name = "seo"
urlpatterns = [
path('scripts', views.Script, name='scripts'),
]
I believe the issue here is in naming collision. Take a closer look to this:
#api_view(['GET'])
def scripts(request):
scripts = Script.objects.all()
serializer = ScriptSerializer(scripts, many=True)
The function named scripts and then this name reassigned with scripts = Script.objects.all(). This may lead to really unexpected behaviours
So try to rename your local variable. I.e.:
#api_view(['GET'])
def scripts(request):
script_items = Script.objects.all()
UPD after urls.py added:
It should point to the function view i.o. model:
path('scripts', views.scripts, name='scripts'),
I wrote Django app and now I'm trying to cover it with automated tests. For testing get_queryset function within my ListView I created a test user and his post, but my test fails with "No User matches the given query". When I execute py manage.py runserver everything is fine, no exceptions are raised and the page's displayed properly. I'm new to Django testing so I absolutely have no idea what's going on. Could you help me please?
This is my view from view.py
class UserPostListView(ListView):
"""Displaying a page with a certain user's posts"""
model = Post
template_name = 'blog/user_posts.html'
context_object_name = 'posts'
paginate_by = 5
def get_queryset(self):
"""Dynamic filtering to get posts by a chosen user"""
queryset = super().get_queryset()
user = get_object_or_404(User, username=self.kwargs.get('username'))
return queryset.filter(author=user).order_by('-date_posted')
Test for that view:
class TestUserPostListView(TestCase):
"""Test UserPostListView"""
def setUp(self):
"""Creating a test user and his post to see if the certain
user's page with posts is displayed properly"""
self.factory = RequestFactory()
self.user = User.objects.create_user(
username='test_user',
email='testuser#example.com',
password='fhhewo87539275'
)
self.post = Post.objects.create(
title='test_post',
content='blabla',
author=self.user
)
def test_get_queryset(self):
"""Testing get_queryset function"""
url = reverse('user-posts', kwargs={'username': self.user.username})
request = self.factory.get(url)
view = UserPostListView()
view.setup(request)
queryset = view.get_queryset()
self.assertIn(self.post, queryset)
Traceback:
Traceback (most recent call last):
File "C:\Users\473491\Documents\django\web_blog\env\lib\site-packages\django\shortcuts.py", line 76, in get_object_or_404
return queryset.get(*args, **kwargs)
File "C:\Users\473491\Documents\django\web_blog\env\lib\site-packages\django\db\models\query.py", line 435, in get
raise self.model.DoesNotExist(django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\473491\Documents\django\web_blog\web_blog\apps\blog\test\test_views.py", line 67, in test_get_queryset
queryset = view.get_queryset()
File "C:\Users\473491\Documents\django\web_blog\web_blog\apps\blog\views.py", line 45, in get_queryset
user = get_object_or_404(User, username=self.kwargs.get('username'))
File "C:\Users\473491\Documents\django\web_blog\env\lib\site-packages\django\shortcuts.py", line 78, in get_object_or_404
raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
django.http.response.Http404: No User matches the given query.
I have found out where the problem was. My UserPostListView is called by the url containing url-variable.
My urlpattern corresponding to the view:
path('users/<str:username>', UserPostListView.as_view(), name='user-posts')
I also use username from the url to filter out posts created by the user with that username by calling get_queryset function in UserPostListView.
I read the docs attentively and found:
When the view is called during the request/response cycle, the setup() method assigns the HttpRequest to the view’s request attribute, and any positional and/or keyword arguments captured from the URL pattern to the args and kwargs attributes, respectively.
It means that instead of view.setup(request) I had to write view.setup(request, username=self.user.username)
When I fixed it the test started running properly and didn't fail.
I'm trying to solve problem described here but code provided in that answer not works for latest django==2.2
I have tried to port this code but failed with that
settings.py:
MIDDLEWARE = ['polls.mymiddleware.CookieMiddleware',
mysite/polls/authbackend.py:
from django.contrib.auth.backends import RemoteUserBackend, UserModel
class Backend(RemoteUserBackend):
def authenticate(**credentials):
"""We could authenticate the token by checking with OpenAM
Server. We don't do that here, instead we trust the middleware to do it.
"""
try:
user = UserModel.objects.get(username=credentials['remote_user'])
print('__user exists')
except UserModel.DoesNotExist:
user = UserModel.objects.create(username=credentials['remote_user'])
print('__user not exists')
# Here is a good place to map roles to Django Group instances or other features.
return user
mysite/polls/mymiddleware.py:
from django.contrib.auth import authenticate, login, ImproperlyConfigured
class CookieMiddleware(object):
"""Authentication Middleware for OpenAM using a cookie with a token.
Backend will get user.
"""
def process_request(self, request):
if not hasattr(request, 'user'):
raise ImproperlyConfigured()
if "thecookiename" not in request.COOKIES:
return
# token = request.COOKIES["thecookiename"]
# REST request to OpenAM server for user attributes.
# token, attribute, role = identity_manager.get_attributes(token)
# user = authenticate(remote_user=attribute['uid'][0])
user = authenticate(remote_user=1) # simplified for test
request.user = user
login(request, user)
result of that:
File "C:\Users\Administrator\Desktop\my_scripts\mysite\mysite\wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "C:\Users\Administrator\Desktop\my_scripts\venv\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "C:\Users\Administrator\Desktop\my_scripts\venv\lib\site-packages\django\core\handlers\wsgi.py", line 135, in __init__
self.load_middleware()
File "C:\Users\Administrator\Desktop\my_scripts\venv\lib\site-packages\django\core\handlers\base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: object() takes no parameters
Update 1: fixed issue above, thanks to #Daniel_Rossman for that:
need to put your middleware after the SessionMiddleware in your MIDDLEWARE settings.
but got now new problem:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\my_scripts\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Administrator\Desktop\my_scripts\venv\lib\site-packages\django\utils\deprecation.py", line 93, in __call__
response = self.process_request(request)
File "C:\Users\Administrator\Desktop\my_scripts\mysite\polls\mymiddleware.py", line 22, in process_request
login(request, user, backend='polls.mymiddleware.CookieMiddleware')
File "C:\Users\Administrator\Desktop\my_scripts\venv\lib\site-packages\django\contrib\auth\__init__.py", line 126, in login
request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
AttributeError: 'NoneType' object has no attribute '_meta'
[25/Apr/2019 12:40:07] "GET /admin/ HTTP/1.1" 500 64298
Inherit yor middleware from MiddlewareMixin like this
from django.utils.deprecation import MiddlewareMixin
class CookieMiddleware(MiddlewareMixin):
"""Authentication Middleware for OpenAM using a cookie with a token.
Backend will get user.
"""
def process_request(self, request):
if not hasattr(request, 'user'):
raise ImproperlyConfigured()
if "thecookiename" not in request.COOKIES:
return
# token = request.COOKIES["thecookiename"]
# REST request to OpenAM server for user attributes.
# token, attribute, role = identity_manager.get_attributes(token)
# user = authenticate(remote_user=attribute['uid'][0])
user = authenticate(remote_user=1) # simplified for test
request.user = user
login(request, user)
I upgraded my project from Django 1.11 to 2.2, made all the changes but came with new error that says login() got an unexpected keyword argument 'template_name'.It worked fine with the previous version of Django 1.11 (All the other urls are working, only the landing page gives the error). I couldn't find any references to this issue. Below is the error and urls and views for the issue.
Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\User\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\User\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\PycharmProjects\MyWebsite\landing\views.py", line 43, in landing_validation
login_response = login(request, template_name='landing.html')
TypeError: login() got an unexpected keyword argument 'template_name'
[19/Apr/2019 16:20:00] "GET / HTTP/1.1" 500 71948
Landing\urls.py
from django.conf.urls import url
from landing.views import landing_validation
app_name='landing'
urlpatterns = [
url(r'^$', landing_validation, name='landing')
]
Landing\views.py
from django.contrib.auth import login
def landing_validation(request):
login_response = login(request, template_name='landing.html')
return login_response
C:\Users\User\venv\Lib\site-packages\django\contrib\auth__init__.py
def login(request, user, backend=None):
"""
Persist a user id and a backend in the request. This way a user doesn't
have to reauthenticate on every request. Note that data set during
the anonymous session is retained when the user logs in.
"""
session_auth_hash = ''
if user is None:
user = request.user
if hasattr(user, 'get_session_auth_hash'):
session_auth_hash = user.get_session_auth_hash()
if SESSION_KEY in request.session:
if _get_user_session_key(request) != user.pk or (
session_auth_hash and
not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash)):
# To avoid reusing another user's session, create a new, empty
# session if the existing session corresponds to a different
# authenticated user.
request.session.flush()
else:
request.session.cycle_key()
try:
backend = backend or user.backend
except AttributeError:
backends = _get_backends(return_tuples=True)
if len(backends) == 1:
_, backend = backends[0]
else:
raise ValueError(
'You have multiple authentication backends configured and '
'therefore must provide the `backend` argument or set the '
'`backend` attribute on the user.'
)
else:
if not isinstance(backend, str):
raise TypeError('backend must be a dotted import path string (got %r).' % backend)
request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
request.session[BACKEND_SESSION_KEY] = backend
request.session[HASH_SESSION_KEY] = session_auth_hash
if hasattr(request, 'user'):
request.user = user
rotate_token(request)
user_logged_in.send(sender=user.__class__, request=request, user=user)
There was no need to do a landing_validation view this line solved the issue.
url(r'^$', LoginView.as_view(template_name='landing.html'), name='landing')
In Django 2.2 app I fixed similar error by using LoginView instead of login.
File users/urls.py:
from django.conf.urls import url
# from django.contrib.auth.views import login
from django.contrib.auth.views import LoginView
from . import views
app_name = 'users'
urlpatterns = [
# url(r'^login/$', login, {'template_name': 'users/login.html'}, name='login'),
url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
The login function that you're using is not actually a view. It's just a regular function which you can use to authenticate a user and set the session cookie, etc.
Read the docs for it's usage: https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.login
Judging by your code, it appears you want to use LoginView.
from django.contrib.auth.views import LoginView
def landing_validation(request):
login_response = LoginView.as_view()(request, template_name='landing.html')
return login_response
You can see that django.contrib.auth.login takes request, user, and an optional backend. There is no template_name. This hasn't changed recently, so I think you would get the same problem in 1.11, too.
It looks like you want a view function, but django.contrib.auth.login is not that. It doesn't return a response.