'Reverse for 'user_page' with keyword arguments '{'pk': ''}' not found. I beleive my decorators are causing the issue
So my login view takes the user to the home.view but first checks user is logged in and then checks if the user is an admin or not through the decorator 'admin_only'. My user will prove to be false for this scenario (which is expected). The user is then redirected to the user_page view to which first goes through the decorator allowed_users (which will be true) and then the user_page view. My aim is to pass the primary key at login_user view to user_page view but the middle men decorators I believe are causing me problems.
views.py
@unauthenticated_user
def login_user (request):
user = None
if request.method == 'POST':
temp_username = request.POST.get('username')
temp_password = request.POST.get('password')
user = authenticate(request, username=temp_username, password=temp_password)
if user is not None:
login(request, user)
context = {'user':user}
print ('Primary key in login view = ', user.id)
return redirect('home', context)
else:
messages.info(request, "Username or Password is incorrect!")
context = {'user':user }
return render(request, 'login.html', context)
@login_required(login_url='login')
@admin_only
def home(request, pk):
user = User.objects.get(id=pk)
context = {'user':user}
return render(request,'home.html',context)
@login_required(login_url='login')
@allowed_users(allowed_roles=['customer'])
def user_page(request,pk):
user = User.objects.get(id=pk)
context = {'user':user}
return render(request, 'user.html', context)
urls.py
#url.py
path('user/<int:pk>/', views.user_page, name='user_page')
path('', views.home, name='home'),
decorators.py
def allowed_users(allowed_roles=[]):
def decorator(allow_users_view_function):
def wrapper_function(request, *args, **kwargs):
group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name
if group in allowed_roles:
user = request.user
pk = user.id
print('PK in allowed_users = ',pk )
return allow_users_view_function(request,pk,*args, **kwargs)
else:
return HttpResponse("You are not authorised to view this page")
return wrapper_function
return decorator
def admin_only(admin_view_function):
def wrapper_function(request, *args, **kwargs):
group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name
if group == 'customer':
print('group = customer')
user = kwargs.get('user')
print ('Primary key in admin_only = ', user.id)
pk = user.id
print('PK in admin_only = ',pk )
context={'pk':pk}
return redirect('user_page', context)
if group == 'admin':
print('group = admin')
return admin_view_function(request, *args, **kwargs)
return wrapper_function
login.html
{% extends 'base.html' %}
{% block content %}
<form action="{% url 'user_page' pk=user.id %}" method="POST"> # error is occuring at this line
{% csrf_token %}
<input type="text" id="username" name="username" placeholder="Enter Username">
<input type="password" id="password" name="password" placeholder="Enter Password">
<input type="submit" value="Login">
</form>
<!--Display error message for any failed login attempt-->
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
Don't have an account? <a href="{% url 'register' %}" >Sign Up</a>
{% endblock %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
