简体   繁体   中英

NoReverseMatch at /signup/ - Reverse for '<WSGIRequest: POST '/signup/'>' not found

The project I am working on is the blogging website and I am stuck at this signup process, I want it to function like after signup, the user lands on the home page, but instead this is showing me the above error

views.py:

def handleSignup(request):
    if request.method == 'POST':
        
        username = request.POST['username']
        fname = request.POST['fname']
        lname = request.POST['lname']
        email = request.POST['email']
        pass1 = request.POST['pass1']
        pass2 = request.POST['pass2']
        
    # creating users

        myuser = User.objects.create_user(username, email, pass1)
        myuser.first_name = fname
        myuser.last_name = lname
        myuser.save()
        messages.success(request, 'your account have been successfully created!')
        
        return redirect(request, "/home.html")

    else:
        return HttpResponse("error 404 not found")

urls.py:

urlpatterns = [
    path("", views.home, name="home"),
    path("contact/", views.contact, name="contact"),
    path("about", views.about, name="about"),
    path("signup/", views.handleSignup, name="handleSignup"),
]

forms in base.html:

          <form action="/signup/" method="post">

            <div class="form-group">
              <label for="username">Username</label>
              <input type="text" class="form-control" id="username" name = 'username' placeholder="choose a unique username">
            </div>

            <div class="form-group">
              <label for="fname">Firstname</label>
              <input type="text" class="form-control" id="fname" name = 'fname' placeholder="First Name">
            </div>

            <div class="form-group">
              <label for="lname">Lastname</label>
              <input type="text" class="form-control" id="lname" name= 'lname' placeholder="Last Name">
            </div>

            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" class="form-control" id="email" name = 'email' placeholder="email@example.com">
            </div>

            <div class="form-group">
              <label for="pass1">Choose Password</label>
              <input type="password" class="form-control" name = 'pass1' id="pass1">
            </div>

            <div class="form-group">
              <label for="pass2">Confirm password</label>
              <input type="password" class="form-control" name = 'pass2' id="pass2">
            </div>
            {% csrf_token %}
            <button type="submit" class="btn btn-primary">Submit</button>

          </form>

the error log:

NoReverseMatch at /signup/
Reverse for '<WSGIRequest: POST '/signup/'>' not found. '<WSGIRequest: POST '/signup/'>' is not a valid view function or pattern name.
Request Method: POST
Request URL:    http://127.0.0.1:8000/signup/
Django Version: 3.1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for '<WSGIRequest: POST '/signup/'>' not found. '<WSGIRequest: POST '/signup/'>' is not a valid view function or pattern name.
Exception Location: C:\Users\jayant nigam\projects\practise\lib\site-packages\django\urls\resolvers.py, line 685, in _reverse_with_prefix
Python Executable:  C:\Users\jayant nigam\projects\practise\Scripts\python.exe
Python Version: 3.8.5
Python Path:    
['C:\\Users\\jayant nigam\\projects\\everythingcs',
 'C:\\Python38\\python38.zip',
 'C:\\Python38\\DLLs',
 'C:\\Python38\\lib',
 'C:\\Python38',
 'C:\\Users\\jayant nigam\\projects\\practise',
 'C:\\Users\\jayant nigam\\projects\\practise\\lib\\site-packages']
Server time:    Mon, 28 Sep 2020 17:39:46 +0000

the error line which it is highlighting:

return redirect(request, "/home.html")

You specified path("", views.home, name="home") in your urls.py , therefore you can just do:

return redirect("/")

You shouldn't be passing request as a first argument to redirect() , you only need to provide a URL (relative or absolute) most of the time.

Actually, as a best practice, that URL should be provided using reverse , eg:

from django.urls import reverse
...
return redirect(reverse('home'))

See the doc for redirect() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM