Unable to use Django Authenticate - python

I am receiving this error:
TypeError at /auth/
authenticate() missing 1 required positional argument: 'request'
I am using Django and also Django Rest Framework.
The HTTP POST looks like this:
{
"username": "HelloUser",
"password": "Hello123"
}
VIEW
#csrf_exempt
#api_view(['POST'])
def authenticate(self, request):
print(request.data)
user = authenticate(username=request.data['username'], password=request.data['password'])
if user is not None:
return Response({'Status': 'Authenticated'})
else:
return Response({'Status': 'Unauthenticated'})
URLS
urlpatterns = [
url(r'^', include(router.urls)),
url(r'create/', views.create),
url(r'retrieve/', views.retrieve),
url(r'auth/', views.authenticate),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Firstly you should not use the name of the view as authenticate as it is getting mixed with Django auth's authenticate
And second, you don't need self parameter in authentication view i.e. def authenticate_user(request)
Update your code like this:
VIEW
from django.contrib.auth import authenticate
#csrf_exempt
#api_view(['POST'])
def authenticate_user(request):
print(request.data)
user = authenticate(username=request.data['username'], password=request.data['password'])
if user is not None:
return Response({'Status': 'Authenticated'})
else:
return Response({'Status': 'Unauthenticated'})
And your urls.py will be updated as per the view's name:
URLS
urlpatterns = [
url(r'^', include(router.urls)),
url(r'create/', views.create),
url(r'retrieve/', views.retrieve),
url(r'auth/', views.authenticate_user),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Related

How to solve Django form redirect not working?

I created a form. I want to when the user fills the form and then sends it, redirect a specific page.
But in my case, the form is saving successfully but does not redirect the page. How can I solve it?
views.py
def setup_wizard(request):
form = SetupForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
setup = form.save()
setup.user = request.user
setup.save()
redirect('dashboard')
else:
form = SetupForm()
context = {
'form': form,
}
return render(request, 'setup_wizard.html', context)
dashboard.urls.py
urlpatterns = [
path('', dashboard, name="dashboard"),
path('setup', setup_wizard, name="setup"),
]
mainproject.urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('dashboard/', include('dashboard.urls')),
... ]
When you're using the redirect method from Django shortcuts you need to return the function result. See this from Django docs

Django return home page in get absolute url

My PostCreateView
class PostCreateView(CreateView):
model = Posts
template_name = "blog/post_form.html"
fields = ["author", "title", "content"]
My urls
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
path("post/new/", PostCreateView.as_view(), name="post-create"),
]
Once I do a post my site crashes since I need to specify a get_absolute_url. I just want to return to the home page but trying
redirect("") fails with error
NoReverseMatch at /post/new/
Reverse for '' not found. '' is not a valid view function or pattern name.
How can I return to the homepage ?
You need to redirect to the view with the given name, so:
redirect('blog-home') # since name='blog-home'
or if you work with a class-based view which has a FormMixin like a CreateView, UpdateView, etc. you reverse with:
from django.urls import reverse
class PostCreateView(CreateView):
# …
def get_success_url(self):
return reverse('blog-home')

How to stop having anonymous user in django

So I have a django project and there is one view(home view) which displays the posts of the followed users and that requires a user to be authenticated, if there is no user loged in, then the code returns a 'AnonimousUser' is not iterable error and I will like my code to redirect the anonymous user to the login page if the person is on the home view. After some investigation I realized that this can be done with a custom middleware but I dont know how to do it so currently that middleware just prints if a user is logged in or if it is anonymous. What can I do to Complete this middleware and get rid of that Anoniomus User error?
middleware.py
class LoginRequiredMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_view(self, request, view_func, view_args, view_kwargs):
if(request.user.is_authenticated):
print(Authenticated")
else:
print("Anonimous user")
views.py
def home(request):
user = Following.objects.get(user=request.user)
followed_users = [i for i in user.followed.all()]
followed_users.append(request.user)
contents = Post.objects.filter(user__in = followed_users)
context = {
"contents": contents,
}
print("nice")
return render(request, 'home.html', context)
urls.py
urlpatterns = [
path('', views.home, name='home'),
path('login', views.login, name='login'),
path('register', views.register, name='register'),
path('logout', views.logout, name='logout'),
path('<username>/', views.profile, name='profile'),
]
If you have any questions or need to see more code please let me know;)
There is no required to writing a custom middleware.
You can restrict the anonymous user in your view like this
def home(request):
if not request.user.is_authenticated:
return redirect ('your_login_url')
user = Following.objects.get(user=request.user)
followed_users = [i for i in user.followed.all()]
followed_users.append(request.user)
OR you can user login_required decorator
from django.contrib.auth.decorators import login_required
#login_required(login_url='your_login_url')
def home(request):
user = Following.objects.get(user=request.user)
followed_users = [i for i in user.followed.all()]
followed_users.append(request.user)
Well, the above answer contains the two solutions to your problem, you could also use django.contrib.auth.mixins, they are used for Class-based view. The links for documentation are
1.https://docs.djangoproject.com/en/3.0/topics/class-based-views/mixins/
2.https://docs.djangoproject.com/en/3.0/topics/auth/default/
One of the possible issue might be that you have set the action of login template form to redirect to some page e.g.
So, it never validates the login VIEW to proceed the authentication. If so, try to set action="" of login form and write the login view which renders the login.html template.

Django “TypeError: list() got an unexpected keyword argument 'id''” error

I am trying to redirect to a page I intend to implement as an object's homepage after creation of one.
views.py
from django.shortcuts import render, get_object_or_404
from f.models import Post
def list(request):
post = Post.objects.all()
context = {
'post': post,
}
return render(request, 'list.html', context)
def detail(request, id=None):
Post = get_object_or_404(post, id=id)
context = {
'Post': Post,
}
return render(request, 'detail.html', context)
url.py
urlpatterns = [
url(r'^$', views.list, name='list'),
url(r'^(?P<id>[0-9]{1,3})$', views.list, name='detail'),
]
and my error
Django Version: 1.9.10
Exception Type: TypeError
Exception Value:
list() got an unexpected keyword argument 'id'
Python Version: 3.5.2
Look at the route:
url(r'^(?P<id>[0-9]{1,3})$', views.list, name='detail'),
You are sending the detail url to the list view views.list instead of the detail view views.detail.
As an aside, it would be better to pick a different name for you list view since list shadows the built-in name list.

Add code to CreateView when using POST method in django

I'm using django's auth module to handle the user login, logout and registration in my website.
The problem is that when I register a new user, I new to redirect to another url, and then, since the model is created but doesn't automatically log in, I need to navigate to /login/ and log in manually.
I would like my site to do this automatically. By that I mean that:
You click on the register button in home.html. You fill in the form and submit it.
The website AUTOMATICALLY creates the new user model, logs you in and redirects you to home.html with the user logged in.
Urls.py looks like this:
from django.conf.urls import include, url, patterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
from django.contrib.auth.forms import UserCreationForm
from browse.views import UserCreate
urlpatterns = patterns('',
url(r'',include('browse.urls')),
url(r'^$', 'browse.views.home_page', name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', auth_views.login, {'template_name': 'browse/login.html'}),
url(r'^register/$', UserCreate.as_view(
template_name='browse/register.html',
form_class=UserCreationForm,
success_url='/register/'
)),
url(r'^logout/$', 'browse.views.logout_view', name='logout'),
url(r'^', include('django.contrib.auth.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and views.py looks like this:
from django.shortcuts import render, redirect
from django.views.generic.edit import CreateView
from browse.models import Pattern
from django.contrib.auth.models import User
from django.contrib.auth import logout, login
class PatternCreate(CreateView):
model = Pattern
fields = ['name','description','license', 'xcheme', 'pictures', 'video']
class UserCreate(CreateView):
model = User
def post(self, request):
return login(request, User.objects.get(username=request.POST['username']))
def home_page(request):
return render(request, 'browse/home.html')
def pattern_detail(request, pk):
pattern = Pattern.objects.get(pk=pk)
return render(request, 'browse/pattern_detail.html', {'pattern': pattern})
def logout_view(request):
logout(request)
return redirect('/')
As you can see, when you GET /register/ the register form is rendered. When I POST to /register/, I want to execute the view as normally AND then add:
def post(self, request):
return login(request, User.objects.get(username=request.POST['username']))
But It looks like I'm overriding the default view instead of adding to it, because the model doesn't get saved, since I get this error:
Exception Value:
User matching query does not exist.
How can I fix it?
Thank you so much beforehand.
Okay, it was a really dumb question. Luckily, I could figure it out:
def post(self, request):
super(UserCreate, self).post(request)
login_username = request.POST['username']
login_password = request.POST['password1']
created_user = authenticate(username=login_username, password=login_password)
login(request, created_user)
return redirect('/')
All I had to do is call super() and the parent function is executed. Then, my code executes.

Categories

Resources