Необъявленный MultiValueDictKeyError в коде регистрации - PullRequest
1 голос
/ 12 октября 2019

Я новичок в Джанго. Я создаю страницу регистрации. Но у меня неожиданный MultiValueDictKeyError со значением исключения fname.

views.py:

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

# Create your views here.
def signup(request):
    if request.method == 'POST':
        # User has and wants account now
        if request.POST['password1'] == request.POST['password2']:
            # Check if username already exists
            try:
                user = User.objects.get(username=request.POST['uname'])
                return render(request, 'accounts/signup.html',{'error':'username already exist'})
            # If username unoccupied
            except User.DoesNotExist:
                user = User.objects.create_user(fname = request.POST['fname'],lname = request.POST['lname'],email = request.POST['email'],uname = request.POST['uname'],password = request.POST['password1'])
                # updating new user
                auth.login(request,user)
                return redirect('home')
    else:
        return render(request,'accounts/signup.html')

def login(request):
    if request.method == 'POST':
        #
        user = auth.authenticate(username = request.POST['username'],password = request.POST['password'])

        if user is not None:

            auth.login(request,user)
            return redirect('home')

        else:

            return render(request, 'accounts/login.html',{'error': 'username or password incorrect'})

    else:
        #
        return render(request,'accounts/login.html')

def logout(request):
    if request.method == 'POST':
        auth.logout(request)
        return redirect('home')

страница регистрации:

{% extends 'base.html'%}

{% block content %}
<div class="container-fluid" style="background-image: linear-gradient(to right, #1a1aff , #000099);padding: 10vh">
  <div class="row">
    <div class="col center" style="color: white;padding: 05vw">
      <h1> Sign Up Now! </h1>
      <br>
      <h2> Become the part world's first personalized <br> Network <h2>
    </div>

    <div class="col-5 center container" style="color:black;padding: 02vw;background-color: white;">
      <span>
        <center>
          <h1>Sign Up</h1>
        </center>
        <br>
      </span>
      <form action="{% url 'signup' %}" method="POST">
        {% csrf_token %}
        {% if error %}
        <p style="color:red "> {{error}} </p>
        {% endif %}
        <h3>First Name</h3>
        <input type="text" id="fname" name="firstname" placeholder="Your name..">
        <br>
        <h3>Last Name</h3>
        <input type="text" id="lname" name="lastname" placeholder="Last Name">
        <br>
        <h3>Email</h3>
        <input type="email" id="email" name="email" placeholder="Email Address">
        <br>
        <h3>Username</h3>
        <input type="text" id="uname" name="uname" placeholder="Username">
        <br>
        <h3>Password</h3>
        <input type="password" id="password" name="password1" placeholder="Password">
        <br>
        <h3>Confirm Password</h3>
        <input type="password" id="password" name="password2" placeholder="Password">
        <br>
        <br>
        <input type="submit" value="Sign Up Now">
      </form>
    </div>

  </div>
</div>

    enter code here

{% endblock %}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...