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

Time:09-28

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

views.py

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)
    user.is_active
    return redirect('home')

...

urls.py

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"),
    ...
]

user-inspect.html

{% 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>
            <br>
            <h3><strong>Name:</strong> {{model.name}}</h3>
        </div>
       
    </div>
</div>
<br><br><br>
<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' model.user.pk %}">
                {% csrf_token %}
                <button type="submit" class="btn btn-primary">Accept</button>
            </form>
        </div>
        

        <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>
        </div>
        
    </div>
</div>




{% endblock %}

models.py

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)
    user.is_active()
    user.save()
    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.

CodePudding user response:

user.is_active is a boolean not a function.

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