Home > Software engineering >  ChangeEmailView() got an unexpected keyword argument 'token'
ChangeEmailView() got an unexpected keyword argument 'token'

Time:10-09

I'm trying to make an url with an value from the views in it

    path('settings/email/changeemail/<str:token>', views.ChangeEmailView , name="changeemail"),

but this error appears if i enter the page:

Internal Server Error: /settings/email/changeemail/0fdb9ef1-ce86-482e-a8ef-3fc202438ba9
Traceback (most recent call last):
 File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
   response = get_response(request)
 File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
   response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
   return view_func(request, *args, **kwargs)
TypeError: ChangeEmailView() got an unexpected keyword argument 'token'

this is my views.py

@login_required(login_url='home:login')
def ChangeEmailView(request):
        if request.method == 'POST':
            objects = User.objects.get(email = request.user.email)
            form = EmailChangingForm(request.POST, instance=objects)
            packet = get_object_or_404(TempUrl, user=request.user)
            token = packet.uid
            if form.is_valid():
                form.save()
                
            return redirect('home:profilesettings')
            
        else:
            objects = User.objects.get(email = request.user.email)
            form = EmailChangingForm(request.POST, instance=objects)
            packet = get_object_or_404(TempUrl, user=request.user)
            token = packet.uid
        context = {'form': form, 'token': token}
        return render(request, 'home/email_settings.html', context)

in the end I want to have an unique url from which the token is saved in the db

heres the other view where the uuid is generated and put to the db

def load_url(request):
   token = uuid.uuid4()
   objects = TempUrl.objects.update_or_create(user = request.user, uid=token, used=False)
   print("Das ist der Token:"   str(token))

   context = {'token': token}



   return render(request, 'home/changeemail_pre.html', context)

CodePudding user response:

You have to add the parameter to the view as well.

def changeEmailView(request, token):
    ...

That's why it's saying unexpected keyword. Because the keyword is there but the view doesn't expect it.

  • Related