我无法访问http:// localhost:8000 / accounts / profile

时间:2017-10-09 06:07:38

标签: python html django

我无法访问http://localhost:8000/accounts/profile。当我访问此地址时,它会将我发送到http://localhost:8000/accounts/login/?next=/accounts/profile/。我真的不明白为什么。我在urls.py写道。

urlpatterns = [
    url(r'^detail$', views.detail,name='detail'),
    url(r'^login/$', views.login,name='login'),
    url(r'^profile/$', views.profile, name='profile'),
]
views.py

中的

def login(request):
    login_form = LoginForm(request.POST)
    regist_form = RegisterForm(request.POST)
    if regist_form.is_valid():
        user = regist_form.save(commit=False)
        context = {
            'user': request.user,
            'login_form': login_form,
            'regist_form': regist_form,
        }

        return redirect('profile', context)

    if login_form.is_valid():
        user = login_form.save(commit=False)
        login(request, user)
        context = {
            'user': request.user,
            'login_form': login_form,
            'regist_form': regist_form,
        }
        return redirect('profile', context)

    context = {
        'login_form': login_form,
        'regist_form': regist_form,
    }
    return render(request, 'registration/accounts/login.html', context)

def profile(request):
    context = {
        'user': request.user,
    }
    return render(request, 'registration/accounts/profile.html', context)

in html

<main>
        <div class="container">
            <div class="detailimg col-xs-12">
                <img class="small_img" src="{% static 'detail.jpg' %}" alt="Detail" />
                <div class="absolute-fill vertical-center-container">
                    <p class="hthree">XXX
                        <span class="hthree_small">
                            <br>YYY
                            <br>ZZZ</span>
                    </p>
                </div>
            </div>

            <div class="bodyele  col-xs-12">
                <a class="button-primary" href="{% url 'accounts:profile' %}">Profile</a>
                <a class="button-primary" href="{% url 'accounts:kenshinresults' %}">See</a>
                <a class="button-primary" href="{% url 'accounts:kenshinresults' %}">Know</a>

            </div>
        </div>
    </main>

当我放置Profile标签时,会发生这种情况。我的代码有什么问题?  urls.pyviews.py&amp; html在帐户中。

2 个答案:

答案 0 :(得分:1)

您需要更改项目settings.py

中的LOGIN_REDIRECT_URL
LOGIN_REDIRECT_URL ='/'

它应该适合你。

答案 1 :(得分:0)

您必须删除return context并替换LOGIN_REDIRECT_URL ='/' 并设置

Set

在settings.py文件中。

我认为这必须解决你的问题。