maxg203 / Django-Tutorials

Source code for my free YouTube series on the Django web framework for Python.
https://www.youtube.com/playlist?list=PLw02n0FEB3E3VSHjyYMcFadtQORvl1Ssj
205 stars 177 forks source link

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

Open bvalgard opened 6 years ago

bvalgard commented 6 years ago

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 commented 6 years ago

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

ahmedibrahim6 commented 6 years ago

`

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 commented 6 years ago

I am getting this now too...

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

baxterjfinch commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

vishnu-chalil commented 6 years ago

Thank you @AndrewOdiit .That has solved the problem

Williey-Masila commented 6 years ago

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 commented 6 years ago

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 notifications@github.com 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 .

Williey-Masila commented 6 years ago

Thanks a lot!

vishnu-chalil commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@AlinaKozz Thank you so much!!!

abkasm commented 6 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

  1. Change return HttpResponseRedirect(reverse("index")) to return redirect(reverse("index")
leGenti commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

leGenti commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

I did it sir but still not working

On Wed, Jul 7, 2021, 11:41 AM Gentian Collaku @.***> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/maxg203/Django-Tutorials/issues/6#issuecomment-875330436, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUYFJWYFHA6UFSVQLN4E6ZLTWPZHZANCNFSM4EWDF2GQ .

MotalibHossain commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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

Shamimgardobuya commented 2 years ago

it worked, i fixed my identation error .

sudisudesh commented 2 years ago

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