skip to Main Content

I keep getting this error even though I have pathed it correctly. Can someone help me? Thanks.

this is my form action url

form method="post" action="/users/" id="archive-form"

this is my javascript fetch url

fetch("/users/", {

this is my urls.py path url

path("users/", views.users, name="users"),

this is my views.py

def users(request):
active_users = User.objects.filter(status='active')
archive_users = User.objects.filter(status='archived')
user_form = UserForm()

if request.method == 'POST':

    user_form = UserForm(request.POST, request.FILES)
    if user_form.is_valid():
        user = user_form.save(commit=False)
        user.save()
        # log_message = f"Added user with ID {user.id}"
        Log.objects.create(user=user.first_name, role=user.job_title, date=timezone.now(), action=log_message)
    # return redirect('app:users')

    print('1')
    user_id = request.POST.get('user_id')
    user = get_object_or_404(User, id=user_id)
    if user:
        if user.status == 'active':
            user.status = 'archived'
            log_message = f"Archived user with ID {user_id}"
        elif user.status == 'archived':
            user.status = 'active'
            log_message = f"Unarchived user with ID {user_id}"
        user.save()
        Log.objects.create(user=user.first_name, role=user.job_title, date=timezone.now(), action=log_message)

    return redirect('app:users')

2

Answers


  1. I believe you should use a relative path like this:

    form method="post" action="./users/" id="archive-form"

    Absolute paths start with /

    Login or Signup to reply.
  2. Only indentation can defines the scope of function. In your code snippet indentation level is not correct for function users.

    Try to check and fix your indentation level in users function in views.py file.

    def users(request):
        active_users = User.objects.filter(status='active')
        archive_users = User.objects.filter(status='archived')
        user_form = UserForm()
    
        if request.method == 'POST':
    
            user_form = UserForm(request.POST, request.FILES)
            if user_form.is_valid():
                user = user_form.save(commit=False)
                user.save()
                # log_message = f"Added user with ID {user.id}"
                Log.objects.create(user=user.first_name, role=user.job_title, date=timezone.now(), action=log_message)
            # return redirect('app:users')
    
            print('1')
            user_id = request.POST.get('user_id')
            user = get_object_or_404(User, id=user_id)
            if user:
                if user.status == 'active':
                    user.status = 'archived'
                    log_message = f"Archived user with ID {user_id}"
                elif user.status == 'archived':
                    user.status = 'active'
                    log_message = f"Unarchived user with ID {user_id}"
                user.save()
                Log.objects.create(user=user.first_name, role=user.job_title, date=timezone.now(), action=log_message)
    
            return redirect('app:users')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search