在注册页面上针对“ <wsgirequest:post =“”'=“” signup =“”'=“”>错误的反向

时间:2019-03-13 06:55:48

标签: python django

我正在尝试创建一个用户帐户,当我使用下面的注册HTML页面上的注册视图和表单进行注册时,一切都很好,但是我收到此错误

  

在/ signup /反向的NoReverseMatch找不到“”。 '' 不是一个   有效的视图函数或模式名称。请求方法:POST请求   URL:http://127.0.0.1:8000/signup/ Django版本:2.1.7异常   类型:NoReverseMatch异常值:找不到“”的反向字符。 '' 不是一个   有效的视图函数或模式名称。例外   位置:C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ lib \ site-packages \ django \ urls \ resolvers.py   在_reverse_with_prefix中,第622行Python   可执行文件:C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ Scripts \ python.exe   Python版本:3.6.8 Python路径:
  ['C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ pastebin',   'C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ Scripts \ python36.zip',   'C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ DLLs',   'C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ lib',   'C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ Scripts',   'c:\ users \ aadeo \ appdata \ local \ programs \ python \ python36 \ Lib',   'c:\ users \ aadeo \ appdata \ local \ programs \ python \ python36 \ DLLs',   'C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory',   'C:\ WINDOWS \ system32 \ dev \ pastebinclonedirectory \ lib \ site-packages']   服务器时间:2019年3月13日星期三06:46:57 +0000

view.py

def signup(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect(request,'app/index.html')
    else:
        form = UserRegisterForm()
    return render(request, 'app/signup.html', {'form': form})

forms.py

from django import forms
from .models import Post
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class Userpostform(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title','content','private',)




class Visitorpostform(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title','content')

urls.py 在apps文件夹中

path('',views.home,name='home'),
path('accounts/', include('django.contrib.auth.urls'),name='login'),
path('signup/',views.signup,name='signup'),
path('accounts/profile/',views.profile,name='profile'),
path('logout',views.logout_view,name='logout'),
path('userpostnew', views.userpostnew, name='userpostnew'),
path('visitorpostnew', views.visitorpostnew, name='visitorpostnew'),

3 个答案:

答案 0 :(得分:1)

您不能添加html这样重定向,

可以像这样使用render

return render(request,'app/index.html')

return redirect('profile') #use name of a view declared in url to redirect to that specific page

答案 1 :(得分:0)

您没有在reverse()中添加redirect

像这样使用它

from django.urls import reverse
    def signup(request):
        if request.method == 'POST':
            form = UserRegisterForm(request.POST)
            if form.is_valid():
                form.save()
                username = form.cleaned_data.get('username')
                raw_password = form.cleaned_data.get('password1')
                user = authenticate(username=username, password=raw_password)
                login(request, user)
                return redirect(reverse('home'))
        else:
            form = UserRegisterForm()
        return render(request, 'app/signup.html', {'form': form})

答案 2 :(得分:0)

  1. 删除request函数中的redirect参数。

    重定向功能定义为:redirect(to, *args, permanent=False, **kwargs):

    它没有request参数。


  2. 使用路径的名称路线代替模板名称。

    在您的情况下:'home''/'而不是'app/index.html'


def signup(request): 
    ...
    if form.is_valid():
       ...
       return redirect('home')
       # or return redirect ('/')