Making the user active when tapping a button in html, and using a DetailView (Django)


I’m trying to make a user active when I tap a button, and I’m using a DetailView.

from .models import Model
from django.contrib.auth.models import User
from django.shortcuts import redirect

class UserInspectView(DetailView):
    model = Model
    template_name = 'user-inspect.html'

# Make the user is_active = True
def accept (request, pk):
    user = User.objects.get(id=pk)
    return redirect('home')


from django.urls import path
from . import views

urlpatterns = [
    path('inspect/<slug:slug>/', views.UserInspectView.as_view(), name='user-inspect'),
    path('inspect/<int:pk>/accept/', views.accept, name="user-accept"),


{% extends 'base.html' %}

{% block title %} <title>User Inspection</title> {% endblock %}

{% block content %}
<div class="d-flex justify-content-center">
    <div class="container d-flex flex-column">
        <div class="ms-5 ps-5" >
            <h3><strong>User:</strong> {{model.user}}</h3>
            <h3><strong>Name:</strong> {{}}</h3>
<div class="d-flex justify-content-center">
    <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
        <div class="btn-group me-2 me-5 pe-5" role="group" aria-label="First group">
            <form method="post" action="{% url 'user-accept' %}">
                {% csrf_token %}
                <button type="submit" class="btn btn-primary">Accept</button>

        <div class="btn-group me-2 me-5 ps-5" role="group" aria-label="First group">
            <a href="" type="button" class="btn btn-secondary">Back</a>

{% endblock %}

from django.db import models
from django.contrib.auth.models import User

    class Model(models.Model):
        name = models.CharField(max_length=100)
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        slug = models.SlugField(unique=True, blank=False, null=False)

Before my accept view looked like this

def accept (request, pk):
    user = User.objects.get(id=pk)
    return redirect('home')

but I changed it, because I got this error

TypeError at inspect/30/accept/

‘bool’ object is not callable

When I tap the Accept button, it takes me to the redirect that I have in the accept view, but the user is still inactive.


user.is_active is a boolean not a function.

def accept (request, pk):
    user = User.objects.get(id=pk)
    user.is_active = True
    return redirect('home')

Answered By – Ene P

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published