9无法在链接中显示正确的页面

9无法在链接中显示正确的页面

本文介绍了Django 1.9无法在链接中显示正确的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

demo / urls.py

demo/urls.py

from django.conf.urls import url, include, patterns
from django.contrib import admin

import views
from . import views

urlpatterns = patterns('demo.urls',
    url(r'^admin', admin.site.urls),
    url(r'^', views.login, name="login"),
    url(r'^resetpwd', views.resetpwd, name='resetpwd'),
    url(r'^mechanics/', include('mechanics.urls', namespace="mechanics")),
)

机械/网址.py

import views

from . import views

urlpatterns = patterns('mechanics.urls',
    url(r'^admin', include(admin.site.urls)),
    url(r'^$', views.login, name="login"),
    url(r'^resetpwd', views.resetpwd, name='resetpwd'),
    url(r'^loandelinquency', views.loandelinquency, name='loandelinquency'),
)

mechanics / views.py

mechanics/views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
import cPickle

def loandelinquency(request):
    return render(request, 'mechanics/LoanDelinquency.html')

def login(request):
    try:
        users = cPickle.load(open('users.p', 'rb'))
    except:
        users = {'[email protected]':'**********'}
        cPickle.dump(users, open('users.p', 'wb'))
    if request.method == 'POST':
        try:
            users = cPickle.load(open('users.p', 'rb'))
        except:
            users = {'[email protected]':'**********'}
            cPickle.dump(users, open('users.p', 'wb'))
        user = request.POST.get('user','')
        pwd = request.POST.get('pwd', '')
        request.session['user'] = user
        request.session['pwd'] = pwd
        try:
            if user in users:
                if users[user] == pwd:
                    return render(request, 'mechanics/inside.html')
                else:
                    wronguser = ''
                    wrongpwd = 'Wrong password.'
                    return render(request, 'mechanics/login.html', {'wrongpwd': wrongpwd, 'wronguser':wronguser})
            else:
                wronguser = 'Wrong username'
                wrongpwd = ''
                return render(request, 'mechanics/login.html', {'wrongpwd': wrongpwd, 'wronguser':wronguser})
        except:
            return render(request, 'mechanics/login.html')
    if request.method == "GET":
        return render(request, 'mechanics/login.html')

def resetpwd(request):
    if request.method == 'GET':
        return render(request, "resetpwd.html")

mechanics / templates / mechanics / login.html

mechanics/templates/mechanics/login.html

<html>
    <body>
        <title>Data Science Demo</title>
            <h1 align="center">Ninja's</h1>
                <br>
                <div align="center"><font size=24>Login</font>
                <br>
                    <form align="center" method="post">
                        {% csrf_token %}
                        E-mail<br>
                        <input type="text" align="center" name="user"><br>
                        Password<br>
                        <input type="password" align="center" name="pwd"><br>
                        <input type="submit" value="Submit">
                    {% if wrongpwd %}
                        <br>
                        <font color="red">{{ wrongpwd }}</font><br>
                    {% endif %}
                    {% if wronguser %}
                        <br>
                        <font color="red">{{ wronguser }}</font>
                    {% endif %}
                    </form>
                    <a href="{% url 'mechanics:resetpwd' %}">Reset Password</a>
                </div>
    </body>
</html>
<style>

div {
    position: relative;
    width: 300px;
    padding: 25px;
    border: 25px solid navy;
    margin: auto;
    background-color: #b0e0e6;
}
</style>

mechanics / templates / mechanics / resetpwd.html

mechanics/templates/mechanics/resetpwd.html

<html>
    <title>Reset Password</title>
        <body>
            <p>You did it!</p>
        </body>
</html>

< a href = {%url'mechanics:resetpwd' %}>重置密码< / a> 只是让我返回登录页面。它与我在代码中插入的所有其他链接相同。我该如何解决?

The <a href="{% url 'mechanics:resetpwd' %}">Reset Password</a> is just returning me to the login page. It does the same with every other link that I've put into to code. How do I fix this?

推荐答案

您需要在正则表达式的末尾添加一个美元符号。

You need to include a dollar sign at the end of your regex.

url(r'^$', views.login, name="login"),

没有美元符号,正则表达式 r'^'将匹配所有网址,因此该网址以下的所有网址格式都将被忽略。

Without the dollar sign, the regex r'^' will match all urls, so any url patterns below this one will be ignored.

您实际上可以从网址中删除该网址和 resetpwd 根url配置,因为您已经将这些视图包含在包含的 mechanics.urls 中。

You can actually remove this url and resetpwd from the root url config, because you already include these views in the included mechanics.urls.

请注意Django随附。您应该使用它而不是尝试自己编写。

Note that Django comes with an authentication system. You should use this rather than trying to write your own.

这篇关于Django 1.9无法在链接中显示正确的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:26