ValueError at /signup/ The given username must be set

空扰寡人 提交于 2021-02-11 17:52:26

问题


so basically I have been building this blogging website and now I am stuck at this point of the Signup process, the whole work is done using django

views.py:

def handleSignup(request):
    if request.method == 'POST':
        # getting user parameters
        username =  request.POST.get('username')
        fname =  request.POST.get('fname')
        lname =  request.POST.get('lname')
        email =  request.POST.get('email')
        pass1 =  request.POST.get('pass1')
        pass2 =  request.POST.get('pass2')
        # fname = request.POST['fname']
        # lname = request.POST['lname']
        # email = request.POST['email']
        # pass1 = request.POST['pass1']
        # pass2 = request.POST['pass2']
        

    # creating users

        myuser = User.objects.create_user(username = username, email = email, password = pass1)
        myuser.first_name = fname
        myuser.last_name = lname
        myuser.save()
        messages.success(request, 'your account have been successfully created!')
        
        return redirect(request, 'home')

    else:
        return HttpResponse("error 404 not found")

form in base.html:

<form action="/signup/" method="post">

            <div class="form-group">
              <label for="username">Username</label>
              <input type="text" class="form-control" id="username" placeholder="choose a unique username">
            </div>

            <div class="form-group">
              <label for="fname">Firstname</label>
              <input type="text" class="form-control" id="fname" placeholder="First Name">
            </div>

            <div class="form-group">
              <label for="lname">Lastname</label>
              <input type="text" class="form-control" id="lname" placeholder="Last Name">
            </div>

            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" class="form-control" id="email" placeholder="email@example.com">
            </div>

            <div class="form-group">
              <label for="pass1">Choose Password</label>
              <input type="password" class="form-control" id="pass1">
            </div>

            <div class="form-group">
              <label for="pass2">Confirm password</label>
              <input type="password" class="form-control" id="pass2">
            </div>
            {% csrf_token %}
            <button type="submit" class="btn btn-primary">Submit</button>

          </form>

now I am getting this error:

ValueError at /signup/
The given username must be set
Request Method: POST
Request URL:    http://127.0.0.1:8000/signup/
Django Version: 3.1
Exception Type: ValueError
Exception Value:    
The given username must be set
Exception Location: C:\Users\jayant nigam\projects\practise\lib\site-packages\django\contrib\auth\models.py, line 135, in _create_user
Python Executable:  C:\Users\jayant nigam\projects\practise\Scripts\python.exe
Python Version: 3.8.5
Python Path:    
['C:\\Users\\jayant nigam\\projects\\everythingcs',
 'C:\\Python38\\python38.zip',
 'C:\\Python38\\DLLs',
 'C:\\Python38\\lib',
 'C:\\Python38',
 'C:\\Users\\jayant nigam\\projects\\practise',
 'C:\\Users\\jayant nigam\\projects\\practise\\lib\\site-packages']
Server time:    Mon, 28 Sep 2020 16:21:27 +0000

as you guys can see in views.py I have already tried fname = request.POST['fname'] but then I was getting the MultiValueDictKeyError for which I searched on net and got a suggestion to use request.POST.get(' ') and after using that I am facing the above error


回答1:


You have to add name to your input field in HTML:

<div class="form-group">
      <label for="username">Username</label>
      <input type="text" class="form-control" id="username" name="username" placeholder="choose a unique username">
</div>

You are not giving name in any of the input field. Server takes data from the form on the basis of name, but not id. So, provide name to each input tag in your form. And, you are good to go.



来源:https://stackoverflow.com/questions/64106353/valueerror-at-signup-the-given-username-must-be-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!