regist()得到了一个意外的关键字参数'template_name'

时间:2017-01-20 14:29:54

标签: python django

我收到了一个错误 / accounts / regist /的TypeError regist()得到了一个意外的关键字参数'template_name'。 我可以理解我的文件中没有变量。 但是,在登录时,不能使用template_name文件。 所以,我不知道如何解决它。

我在urls.py中写道,

from django.conf.urls import url
from . import views
from django.contrib.auth.views import login, logout

urlpatterns = [
    url(r'^login/$', login,
        {'template_name': 'registration/accounts/login.html'},
        name='login'),
    url(r'^logout/$', logout, name='logout'),
    url(r'^regist/$', views.regist,
        {'template_name': 'registration/accounts/regist.html'},
        name='regist' ),
    url(r'^regist_save/$', views.regist_save, name='regist_save'),
]
在views.py中

from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.decorators.http import require_POST
from .forms import RegisterForm



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


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


def regist(request):
    form = RegisterForm(request.POST or None)
    context = {
        'form': form,
    }
    return render(request, 'accounts/regist.html', context)


@require_POST
def regist_save(request):
    form = RegisterForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('main:index')

    context = {
        'form': form,
    }
    return render(request, 'accounts/regist.html', context)

如果我必须定义template_name,我应该写哪个文件以及如何编写? 是否存在制作系统登录页面和注册页面的差异?

1 个答案:

答案 0 :(得分:2)

当您使用自己的基于函数的视图时,不必指定template_name,因此只需从注册URL中删除{'template_name': 'registration/accounts/regist.html'}位。