Django - NoReverseMatch在/

时间:2016-03-31 20:10:02

标签: django python-3.x django-registration

我正在尝试为我的Django项目实现和登录和注册系统。我目前收到此错误...

NoReverseMatch at /
Reverse for 'register' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

以下是我的一些代码,希望你能提供帮助......

Urls.py

urlpatterns = [
url(r'^stocks/$', StockView.as_view(), name='stocks'),
url(r'^$', IndexView.as_view(), name="index"),
#url(r'^data/$', DataView.as_view(), name='data'),
url(r'^quizzes/$', DefView.as_view(), name='quizzes'),
url(r'^tickers/$', DefView.as_view(), name='ticker'),

url(r'^accounts/login/$', 'stockmarket.views.login'),
url(r'^accounts/auth/$', 'stockmarket.views.auth_view'),
url(r'^accounts/logout/$', 'stockmarket.views.logout'),
url(r'^accounts/loggedin/$', 'stockmarket.views.loggedin'),
url(r'^accounts/invalid/$', 'stockmarket.views.invalid_login'),

url(r'^accounts/register/$', 'stockmarket.views.register_user'),
url(r'^accounts/register_success/$', 'stockmarket.views.register_success'),

Views.py

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from django.contrib.auth.forms import UserCreationForm


def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('login.html', c)


def auth_view(request):
    username = reqruest.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/loggedin')
    else:
        return HttpResponseRedirect('/accounts/invalid')


def loggedin(request):
    return render_to_response('loggedin.html', {'full_name': request.user.username})


def invalid_login(request):
    return render_to_response('invalid_login.html')


def logout(request):
    auth.logout(request)
    return render_to_response('logout.html')


def register_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')

    args = {}
    args.update(csrf(request))
    args['form'] = UserCreationForm()

    return render_to_response('register.html', args)


def register_success(request):
    return render_to_response('register_success.html')

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在项目的某个地方,您使用django的urlresolvers返回url以使用名为'register'的命名网址。

看到您的urls.py后,您似乎没有任何指定了register命名的网址。你应该把它命名为合适的地方..像这样:

url(r'^accounts/register/$', 'stockmarket.views.register_user', name='register'),