Django-Tutorials icon indicating copy to clipboard operation
Django-Tutorials copied to clipboard

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

Open bvalgard opened this issue 7 years ago • 39 comments

Hi Max,

Thank you for your youtube videos and making your materials available.

I keep running into a value error. In the Django Tutorial Part 15 I was getting the following issue: "The view accounts.views.register didn't return an HttpResponse object. It returned None instead."

I cloned this repository so I could see where I was making the mistake. The same issue occurs in the cloned repository as well.

These are the steps I take to get the error. I make migrations, then run the server. Then I go to the website and click on register, I add in the credentials (username, first name, etc.) then click submit. The website always raises: "ValueError at /account/register/" "The view accounts.views.register didn't return an HttpResponse object. It returned None instead."

I am using windows 10.

Any help would be greatly appreciated! Thanks, Bradon

bvalgard avatar Mar 19 '18 19:03 bvalgard

i have the same problem, can you help me? i think the problem is in register function, in the views

JoaoGarcez avatar May 02 '18 13:05 JoaoGarcez

  • there is some error in indentation in the last two lines of register function in views.py
  • and there is no url named home in accounts urls so we do the following edit

`

if request.method =='POST':
    form = RegistrationForm(request.POST)
    if form.is_valid():
        form.save()
        username = request.POST.get('username')
        password = request.POST.get('password1')
        user = authenticate(
            request,
            username = username,
            password = password
        )
        login(request, user)
        return redirect(reverse('home:home'))
else:
    form = RegistrationForm()
args = {'form': form}
return render(request, 'accounts/reg_form.html', args)`

@bvalgard @JoaoGarcez

ahmedibrahim6 avatar May 05 '18 04:05 ahmedibrahim6

I am getting this now too...

It was working fine up until I tried registering recently. Not sure what broke it.

baxterjfinch avatar May 31 '18 17:05 baxterjfinch

This was a silly mistake on my part. I copy/pasted from my 'login.html' file.

I forgot to close off my

in both 'login.html' and 'reg_form.html'. The login worked but registering did not. Adding
fixed my problem.

baxterjfinch avatar May 31 '18 18:05 baxterjfinch

I'am getting this error type object 'UserProfile' has no attribute 'objects' when trying to registering new user.When I resend data I get The view accounts.views.register didn't return an HttpResponse object. It returned None instead. I don't understand what has gone wrong. Somebody please help me.

vishnu-chalil avatar Jul 03 '18 05:07 vishnu-chalil

@vishnu-chalil try adding ,"objects = models.Manager()" in your UseProfileModel

aodr3w avatar Jul 03 '18 13:07 aodr3w

Thank you @AndrewOdiit .That has solved the problem

vishnu-chalil avatar Jul 04 '18 06:07 vishnu-chalil

I also encountered the same problem "The view accounts.views.register didn't return an HttpResponse object. It returned None instead" when I hit submit Button. I tried to use the above provided solutions but i got no change. Kindly help me. Thanks!

Williey-Masila avatar Jul 04 '18 17:07 Williey-Masila

Check the indentation levels. remember that a view is always supposed to return a render(request, 'template name')

On Wed, Jul 4, 2018, 8:58 PM Williey-Masila [email protected] wrote:

I also encountered the same problem "The view accounts.views.register didn't return an HttpResponse object. It returned None instead" when I hit submit Button. I tried to use the above provided solutions but i got no change. Kindly help me. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/maxg203/Django-Tutorials/issues/6#issuecomment-402535415, or mute the thread https://github.com/notifications/unsubscribe-auth/Ae01zURbYzTnAHm6b1PqfvcUudQ7B9rBks5uDQJAgaJpZM4Swy6N .

aodr3w avatar Jul 04 '18 18:07 aodr3w

Thanks a lot!

Williey-Masila avatar Jul 15 '18 14:07 Williey-Masila

why cant I access the Userprofile with user.userprofile . It gives me error "User has no userprofile". RelatedObjectDoesNotExist at /account/register/.Can someone please help.

vishnu-chalil avatar Jul 22 '18 07:07 vishnu-chalil

Hi Man! I am facing the same problem: ValueError: The view accounts.views.register didn't return an HttpResponse object. It returned None instead. Please could anyone assist me how to fix that I am using django 2.0

Thank you!

ghost avatar Jul 23 '18 05:07 ghost

@Jumaev `class RegisterView(View):

def post(self, request):
    form = UserCreateForm(request.POST)

    if form.is_valid():
        form.save()
        return redirect(reverse(home))
    return render(request, 'accounts/register.html', {'form': form})

def get(self, request):
    form = UserCreateForm()
    return render(request, 'accounts/register.html', {'form': form})`

akazyryna avatar Jul 26 '18 01:07 akazyryna

@AlinaKozz Thank you so much!!!

ghost avatar Jul 26 '18 09:07 ghost

because you handled the validated data ... and you did'nt put an else statement to handle the non validated data ... so your view function returns a non object instead of an http response ... the simple solution is to add an else statement ... good luck !! like if form.is_valide(): .... return redirect(...) else: return render(....)

abkasm avatar Aug 13 '18 23:08 abkasm

the problem is that the code should be : ef register(request): if request.method =='POST': form = RegisterationFrom(request.POST) if form.is_valid(): form.save() return redirect('/home') else: form = RegisterationFrom() args = {'form': form} return render(request, 'account/reg_from.html', args)

cuz if u didn`t do this it and do this : def register(request): if request.method =='POST': form = RegisterationFrom(request.POST) if form.is_valid(): form.save() return redirect('/home') else: form = RegisterationFrom() args = {'form': form} return render(request, 'account/reg_from.html', args)

the render function will execute when the value is false , but when it `s True it will not have a template to render then it will return none

hatem8311 avatar Sep 30 '18 07:09 hatem8311

I had the same issue and i solved it , you just Edit in Class Meta: in forms.py

class Meta(UserCreationForm.Meta): model = User fields = UserCreationForm.Meta.fields + ( 'username', 'first_name', 'last_name', 'email', 'password1', 'password2' )

check the docs : Custom users and the built-in auth forms

GeekAhmeds avatar Nov 02 '18 06:11 GeekAhmeds

from django.shortcuts import render,redirect from django.http import HttpResponse from .models import List from .forms import ListForm from django.contrib import messages

def index(request): if request.method == 'POST': form = ListForm (request.POST or None);

	if form.is_valid():
		form.save()
		all_items = List.objects.all
		messages.success(request, ('Items has been added to list'));
		return render(request, 'home.html', {'all_items': all_items});
else:
		all_items = List.objects.all
		return render(request,'home.html', {'all_items' : all_items});		

Sreekanth73 avatar Jul 04 '19 14:07 Sreekanth73

whenever I click the submit button it gives an The view todo_list.views.index didn't return an HttpResponse object. It returned None instead.

how can i correct it..? can any one help me..?

Sreekanth73 avatar Jul 04 '19 14:07 Sreekanth73

hello developers ... I have a problem I am doing CRUD operation in Django python but I am facing the problem

Screenshot (142) when I click on this link or add details still I have no records on the database I want to add new ...and I am getting this type off an error when I move from show to emp for add details Screenshot (141) here is my form code Screenshot (146) here are my Views code Screenshot (143) here is my html Screenshot (144) here is my urls Screenshot (145) is anybody here to solve this.........

hapSa000 avatar Jul 18 '19 07:07 hapSa000

I'm getting the same error: The view users.views.login_view didn't return an HttpResponse object. It returned None instead.

However I don't see any syntax error or formatting errors:

def login_view(request): if request.method == "POST": username = request.POST["username"] password = request.POST["password"] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return HttpResponseRedirect(reverse("index")) else: return render(request, "users/login.html", { "message": "Invalid credentials." })

StotleD avatar Jul 18 '20 01:07 StotleD

I'am getting this error type object 'UserProfile' has no attribute 'objects' when trying to registering new user.When I resend data I get The view accounts.views.register didn't return an HttpResponse object. It returned None instead. I don't understand what has gone wrong. Somebody please help me. it is because of that you have not linked to the admin page

Prashanthkumar1998 avatar Oct 06 '20 12:10 Prashanthkumar1998

Guys, this project methodology is already expired, many things had been changed since he published his tutorials, I'm not even sure if he still alive or not

hazho avatar Oct 09 '20 19:10 hazho

I'm getting the same error: The view users.views.login_view didn't return an HttpResponse object. It returned None instead.

However I don't see any syntax error or formatting errors:

def login_view(request): if request.method == "POST": username = request.POST["username"] password = request.POST["password"] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return HttpResponseRedirect(reverse("index")) else: return render(request, "users/login.html", { "message": "Invalid credentials." })

First of, is this code indented properly on your end??

If yes then do this: 1.Import redirect with this line >>> from django.shortcuts import redirect 2. Change return HttpResponseRedirect(reverse("index")) to return redirect(reverse("index")

maro-okegbero avatar Oct 09 '20 20:10 maro-okegbero

hello developers ... I have a problem I am doing CRUD operation in Django python but I am facing the problem

Screenshot (142) when I click on this link or add details still I have no records on the database I want to add new ...and I am getting this type off an error when I move from show to emp for add details Screenshot (141) here is my form code Screenshot (146) here are my Views code Screenshot (143) here is my html Screenshot (144) here is my urls Screenshot (145) is anybody here to solve this.........

Try putting a form tag around your 'tr' tag. U are using a post method but you did not define it in your html. So your view doesn't know which post it has to handle.

{% for... } form method="post" {%csrf_token%} tr data /tr /form {%endfor%}

leGenti avatar Jan 15 '21 10:01 leGenti

Hi everyone! I have faced a problem in the below code:

def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form})

I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

HaseebMabood avatar Jul 07 '21 04:07 HaseebMabood

the error is "ValueError at /register The view user.views.register didn't return an HttpResponse object. It returned None instead."

HaseebMabood avatar Jul 07 '21 04:07 HaseebMabood

Hi everyone! I have faced a problem in the below code:

def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form})

I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

Hi everyone! I have faced a problem in the below code:

def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form})

I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

You have a type error in your return redirect(reverse(registraTion))

leGenti avatar Jul 07 '21 06:07 leGenti

Hi everyone! I have faced a problem in the below code: def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form}) I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

Hi everyone! I have faced a problem in the below code: def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form}) I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

You have a type error in your return redirect(reverse(registraTion))

so what should I do now Sir ,Please help me

HaseebMabood avatar Jul 07 '21 06:07 HaseebMabood

Hi everyone! I have faced a problem in the below code: def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form}) I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

Hi everyone! I have faced a problem in the below code: def register(request): if request.method == 'POST': user_form = UserRegistration(request.POST) if user_form.is_valid(): user_form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect(reverse('registraion')) else: reg_form = UserRegistration() return render(request,'registration.html',{'reg_form':reg_form}) I have indented it properly but still doesn't work.If anyone knows this, Please help me out.

You have a type error in your return redirect(reverse(registraTion))

so what should I do now Sir ,Please help me

return redirect('registraion.html') Is it Okay now?

HaseebMabood avatar Jul 07 '21 06:07 HaseebMabood