Search code examples
djangodjango-viewsdjango-templatesdjango-urls

Keep getting this error when trying to render my custom HTML form


Here is the error I got when I removed the else clause from my views.py file.

  File "C:\Users\david\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 309, in check_response      
raise ValueError(
ValueError: The view register.views.register didn't return an HttpResponse object. It returned None instead. 

I'm guessing that I got this error as there is no else clause, which I need to have but if I have it in my views.py file I get another error.

Here is my view file:

from django.shortcuts import render,redirect
from django.contrib.auth.models import User, auth

def register(request):
    if request.method == 'POST':
        first_name = request.POST['first_name'],
        last_name = request.POST['last_name'],
        dob = request.POST['dob'],
        email = request.POST['email']
        newuser= User.objects.create_user(first_name=first_name,last_name=last_name, email=email)
        newuser.save()
        print(request.POST)
        return redirect('/home')

I originally had an else clause at the bottom of this but that caused a different error.

Here is that clause:

else: 
    return render('/register/userinfo.html')

And the error I get with it in place:

 File "C:\Users\david\OneDrive\Desktop\Django\Sub\register\views.py", line 4, in render      
    return render(request,'/register/userinfo.html')
TypeError: render() takes 1 positional argument 
but 2 were given

Any idea as to what may cause this bottom error?

Edit 1: Just added in a request clause to the return render and got another error, this time:

  File "C:\Users\david\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py", line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: register/userinfo.html 

Looks like it can't find my template I used for the form. It's path is set up so that it is in a template folder within my app of register.

Edit 2 Got the form to show up, but when I submit I get this Page not found error:

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/register/register
Using the URLconf defined in Main.urls, Django tried these URL patterns, in this order:

admin/
home/
about/
addinfo/
register/ [name='register']
The current path, register/register, didn’t match any of these.

Solution

  • You have to check your path to userinfo.html. You should not start with /. Try this:

    return render(request,'userinfo.html')