Home > Blockchain >  Django throws an error while trying access registration page
Django throws an error while trying access registration page

Time:05-28

Django throws the following at me while I try to access the signup page..

The view accounts.views.signup_view didn't return an HttpResponse object. It returned None instead.

I think everything was done right but apparently django doesn't agree.

Signup.html

<h3>Register</h3>

<form method="POST" action="">
    {% csrf_token %}
    {{form.as_p}}

    <input type="submit" name="Create User">
</form>

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('signup/', views.signup_view, name='signup'),
]

views.py

from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm


# Create your views here.
def signup_view(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()

        context = {'form': form}
        return render(request, 'signup.html', context)

Edit:

def signup_view(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request, f'Account created for {username}!')
            return redirect('new')
    else:
        form = UserCreationForm()
        return render(request, 'signup.html', {'form': form})

After redirecting it doesn't let me access the requested page.. in this case 'new'

Reverse for 'new' not found. 'new' is not a valid view function or 

NoReverseMatch at /accounts/signup/
Reverse for 'new' not found. 'new' is not a valid view function or pattern name.
Request Method: POST
Request URL:    http://127.0.0.1:8000/accounts/signup/
Django Version: 4.0.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'new' not found. 'new' is not a valid view function or pattern name.

I've confirmed the url 'new' does exist

CodePudding user response:

On the first line of signup_view, you have if request.method == "POST": So if the request method isn't POST, the view returns None.

CodePudding user response:

def signup_view(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Account created for {username}!')
            return redirect('http://localhost:8000')
    else:
        form = UserCreationForm()
        return render(request, 'signup.html', {'form': form})

I needed to use redirect and save in order for this to work.. but even though this question got anwsered there's still one small thing

My homepage exists under the url ' ' (empty) and I am not able to redirect to it so instead localhost:8000 is being used, how am i able to replace it?

Found this anwser aswell, for some reason python or django is sensitive to indentation

def signup_view(request):
        if request.method == 'POST':
            form = UserCreationForm(request.POST)
            if form.is_valid():
                form.save()
                username = form.cleaned_data.get('username')
                messages.success(request, f'Account created for {username}!')
                return redirect('http://localhost:8000')

should be

if request.method == 'POST':
 form = UserCreationForm(request.POST)
                if form.is_valid():
                    form.save()
                    username = form.cleaned_data.get('username')
                    messages.success(request, f'Account created for {username}!')
                return redirect('http://localhost:8000')
  • Related