django Not Found:/user/

aij0ehis  于 2023-03-24  发布在  Go
关注(0)|答案(2)|浏览(146)

我一直得到这个错误,即使我已经正确地路径。有人能帮助我吗?谢谢。
这是我的表单操作URL
form method=“post”action="/users/”id=“archive-form”
这是我的javascript fetch url
fetch(“/users/",{
这是我的urls.py路径url
path(“users/",views.users,name=“users”),
这是我的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')
juzqafwq

juzqafwq1#

我认为您应该使用如下的相对路径:
form method="post" action="./users/" id="archive-form"
绝对路径以/开头

1rhkuytd

1rhkuytd2#

只有缩进可以定义函数的作用域。在您的代码段中,函数users的缩进级别不正确。
尝试检查并修复views.py文件中users函数的缩进级别。

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')

相关问题