Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bvalgard opened this issue Mar 19, 2018 · 39 comments

Comments

@bvalgard
Copy link

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

@JoaoGarcez
Copy link

JoaoGarcez commented May 2, 2018

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

@ahmedibrahim6
Copy link

ahmedibrahim6 commented May 5, 2018

  • 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

@baxterjfinch
Copy link

I am getting this now too...

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

@baxterjfinch
Copy link

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.

@vishnu-chalil
Copy link

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.

@aodr3w
Copy link

aodr3w commented Jul 3, 2018

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

@vishnu-chalil
Copy link

Thank you @AndrewOdiit .That has solved the problem

@Williey-Masila
Copy link

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!

@aodr3w
Copy link

aodr3w commented Jul 4, 2018 via email

@Williey-Masila
Copy link

Thanks a lot!

@vishnu-chalil
Copy link

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.

@ghost
Copy link

ghost commented Jul 23, 2018

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!

@akazyryna
Copy link

@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})`

@ghost
Copy link

ghost commented Jul 26, 2018

@AlinaKozz Thank you so much!!!

@abkasm
Copy link

abkasm commented Aug 13, 2018

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(....)

@hatem8311
Copy link

hatem8311 commented Sep 30, 2018

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

@GeekAhmeds
Copy link

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

@Sreekanth73
Copy link

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
Copy link

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..?

@hapSa000
Copy link

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.........

@StotleD
Copy link

StotleD commented Jul 18, 2020

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."
})

@Prashanthkumar1998
Copy link

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

@hazho
Copy link

hazho commented Oct 9, 2020

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

@maro-okegbero
Copy link

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")

@leGenti
Copy link

leGenti commented Jan 15, 2021

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%}

@HaseebMabood
Copy link

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
Copy link

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

@leGenti
Copy link

leGenti commented Jul 7, 2021

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))

@HaseebMabood
Copy link

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
Copy link

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?

@leGenti
Copy link

leGenti commented Jul 7, 2021

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

Change return redirect(reverse(registraion)) to return redirect(reverse(registration)).

@HaseebMabood
Copy link

HaseebMabood commented Jul 7, 2021 via email

@MotalibHossain
Copy link

hello developers ... I have a problem I am doing CRUD operation in Django python but I am facing the problem.
Screenshot (173)
Screenshot (174)
Screenshot (175)
Screenshot (176)
Screenshot (177)

@leGenti
Copy link

leGenti commented Jul 19, 2021

Screenshot (175)

Try putting a forward slash ( / ) behind form and success and put .as_view() behind your views.

path('form/', views.stuForm.as_view(), name='Stu_form'),
path('succes/', views.success.as_view(), name='success'),

Hope this helps

@ahmedsharifkhan
Copy link

ahmedsharifkhan commented Sep 3, 2021

Believe me or not It is an indentation error

this is my code

def contact(request):
if request.method=="POST":
name=request.POST['name']
email=request.POST['email']
phone=request.POST['phone']
content =request.POST['content']
if len(name)<2 or len(email)<3 or len(phone)<10 or len(content)<4:
messages.error(request, "Please fill the form correctly")
else:
contact=Contact(name=name, email=email, phone=phone, content=content)
contact.save()
messages.success(request, "Your message has been successfully sent")
return render(request, 'mywebsite/contact.html')

Now lock at those pictures below

if I use this one I get the same error which you mention above

Screenshot 2021-09-04 003708

ValueError at /contact
The view mywebsite.views.contact didn't return an HttpResponse object. It returned None instead.

But This image is the same code as I have given above. Look at this very carefully the position of **if ** minor move under the contact

Screenshot 2021-09-04 004330

@maheshbist111
Copy link

getting this error can plese anyone help
ValueError at /orders/place_order/

The view orders.views.place_order didn't return an HttpResponse object. It returned None instead.

and This is my code ...
def place_order(request, total=0, quantity=0,):

current_user = request.user

cart_items = CartItem.objects.filter(user=current_user)
cart_count = cart_items.count()
if cart_count <= 0:
    return redirect('store')

grand_total = 0
tax = 0
for cart_item in cart_items:
    total += (cart_item.product.price*cart_item.quantity)
    quantity += cart_item.quantity
tax = (12 * total)/100
grand_total = total + tax

if request.method == 'POST':
    form = OrderForm(request.POST)
    if form.is_valid():
        # store all the information inside belling table
        data = Order()
        data.user = current_user
        data.first_name = form.cleaned_data['first_name']
        data.last_name = form.cleaned_data['last_name']
        data.phone = form.cleaned_data['phone']
        data.email = form.cleaned_data['email']
        data.address_line_1 = form.cleaned_data['address_line_1']
        data.address_line_2 = form.cleaned_data['address_line_2']
        data.country = form.cleaned_data['country']
        data.provence = form.cleaned_data['provence']
        data.zone = form.cleaned_data['zone']
        data.district = form.cleaned_data['district']
        data.order_note = form.cleaned_data['order_note']
        data.order_total = grand_total
        data.tax = tax
        data.ip = request.META.get('REMOTE_ADDR')
        data.save()
        # genarate order no
        yr = int(datetime.date.today().strftime('%y'))
        dt = int(datetime.date.today().strftime('%d'))
        mt = int(datetime.date.today().strftime('%m'))
        d = datetime.date(yr,mt,dt)
        current_date = d.strftime("%y%m%d")
        order_number = current_date + str(data.id)
        data.order_number = order_number
        data.save()
        return redirect('checkout')
else:
    return redirect('checkout')

@Shamimgardobuya
Copy link

Hey Maheshbist111 got the same error as well ,lemme try to debug it.

@Shamimgardobuya
Copy link

it worked, i fixed my identation error .

@sudisudesh
Copy link

Hello Shamimgardobuya how you fix that error i written code in proper indentation but i got a error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests