如何停止在Django中获取此“找不到” URL路由错误

时间:2019-02-07 20:45:47

标签: python django

尽管两个链接完全相同,但我不断收到“在/ accounts / login / next上找不到页面?”单击链接时出错。我的代码有什么错误? “探索”有效,但“发生”无效!请帮忙!

views.py

def explore(request):
    return render(request, 'explore.html')

def happening(request):
    return render(request, 'happening.html')

html模板

  <div id="happening_log">
     <a style= "padding-left:5px" href="{% url 'happening' 
          %}">Happening</a>
  </div>

urls.py

urlpatterns = [
     path('', views.explore, name='explore'),
     path('<user__name>/', views.home, name='home'),
     path('happening/', views.happening, name='happening'),
]

找不到页面(404) 请求方法:GET 要求网址:http://127.0.0.1:8000/accounts/login/?next=/happening/ Django使用mysite.urls中定义的URLconf,按以下顺序尝试了以下URL模式:

admin/
[name='explore']
<user__name>/ [name='home']
/happening/ [name='happening']
users/
^static\/(?P<path>.*)$
^media\/(?P<path>.*)$
The current path, accounts/login/, didn't match any of these.

1 个答案:

答案 0 :(得分:0)

您是否在项目站点中创建了一个registration / login.html文件? 网址http://127.0.0.1:8000/accounts/login/?next=/happening/正在访问登录模板,因此您必须进行默认的registration / login.html或其他类似如下的登录;

def login(request):

    if request.method == 'POST':
        form = LoginForm(request.POST)

        if form.is_valid():
            username=form.cleaned_data.get('username')
            password=form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)

            if user and user.profile.status == 'WORKING':
                auth.login(request, user)

                if user.profile.passwd:
                    return redirect('myprofile')
                else:
                    return redirect('password')
    else:
        form = LoginForm()

    return render(request, 'accounts/login.html', {'form': form})