未找到“user_login”的 NoReverseMatch / Reverse。

Django 2 新手。我已经检查了所有与此错误相关的早期帖子,但找不到我的错误。以下是详细信息:项目-learning_users;应用程序 - basic_app。有 4 个模板 - base.html、index.html、login.html、注册。html。


views.py


from basic_app.forms import UserForm, UserProfileInfoForm


from django.contrib.auth import authenticate, login, logout

from django.http import HttpResponse, HttpResponseRedirect

from django.urls import reverse

from django.contrib.auth.decorators import login_required


# Views


def index(request):

    return render(request, 'basic_app/index.html')


@login_required

def special(request):

    return HttpResponse("You are logged in, Nice !")


@login_required

def user_logout(request):

    logout(request)

    return HttpResponseRedirect(reverse('index'))


def register(request):

    registered = False

    if request.method == "POST":

        user_form = UserForm(data=request.POST)

        profile_form = UserProfileInfoForm(data=request.POST)


        if user_form.is_valid and profile_form.is_valid:

            user = user_form.save()

            user.set_password(user.password)

            user.save()


            profile = profile_form.save(commit=False)

            profile.user = user


            if 'profile_pic' in request.FILES:

                profile.profile_pic = request.FILES['profile_pic']

            profile.save()

            registered = True


        else:

            print(user_form.errors, profile_form.errors)

    else:

        user_form = UserForm()

        profile_form = UserProfileInfoForm()


    return render(request, 'basic_app/registration.html',

            {'user_form':user_form,

             'profile_form':profile_form,

            'registered':registered})

偶然的你
浏览 73回答 1
1回答

当年话下

在你的login.html:<form&nbsp;method="post"&nbsp;action="{%&nbsp;url&nbsp;'basic_app:login'&nbsp;%}"&nbsp;>在你的base.html:<li><a&nbsp;class="nav-link"&nbsp;href="{%&nbsp;url&nbsp;'basic_app:login'&nbsp;%}">Login</a></li>例如,您的名字是:login在您的urls.py:path('user_login/',&nbsp;views.user_login,&nbsp;name='login')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python