title: Why am I getting this error? AttributeError: 'str' object has no attribute 'decode'
body: <p>I am trying to create a registration page with email verification. I am new in Python Dajngo webdevelopment. Currently I using Python 3.6, Django 2.2.4, Postgresql 11 and Ubuntu OS. But I am having a problem and can't figure it out. Guys could you please help me to solve the problem? Thanks in advance!</p>
  
  <p>I have provided all the code bellow:</p>
  
  <p><strong>Here is the Error I am getting:</strong></p>
  
  <pre><code>AttributeError at /register/
  'str' object has no attribute 'decode'
  Request Method: POST
  Request URL:    http://127.0.0.1:8000/register/
  Django Version: 2.2.4
  Exception Type: AttributeError
  Exception Value:    
  'str' object has no attribute 'decode'
  Exception Location: /media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/chhichhi_project/users/views.py in register, line 36
  Python Executable:  /media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/env/chhichhi/bin/python
  Python Version: 3.6.8
  Python Path:    
  ['/media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/chhichhi_project',
   '/usr/lib/python36.zip',
   '/usr/lib/python3.6',
   '/usr/lib/python3.6/lib-dynload',
   '/media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/env/chhichhi/lib/python3.6/site-packages']
  Server time:    Wed, 28 Aug 2019 09:37:05 +0000
  </code></pre>
  
  <p><strong>This is settings.py file</strong></p>
  
  <pre><code>"""
  Django settings for chhichhi_project project.
  
  Generated by 'django-admin startproject' using Django 2.2.3.
  
  For more information on this file, see
  https://docs.djangoproject.com/en/2.2/topics/settings/
  
  For the full list of settings and their values, see
  https://docs.djangoproject.com/en/2.2/ref/settings/
  """
  
  import os
  
  # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  
  
  # Quick-start development settings - unsuitable for production
  # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
  
  # SECURITY WARNING: keep the secret key used in production secret!
  SECRET_KEY = "2ba!+2akp+w%d_dfhj)u_@+rg&amp;t8)r$&amp;uyfwza+cza4jv55cyr"
  
  # SECURITY WARNING: don't run with debug turned on in production!
  DEBUG = True
  
  ALLOWED_HOSTS = []
  
  
  # Application definition
  
  INSTALLED_APPS = [
      'users.apps.UsersConfig', # new
      'comments.apps.CommentsConfig', # new
      'blog.apps.BlogConfig', # new    
      'crispy_forms', # 3rd party form styler
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
  
  ]
  
  MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  ]
  
  ROOT_URLCONF = 'chhichhi_project.urls'
  
  TEMPLATES = [
      {
          'BACKEND': 'django.template.backends.django.DjangoTemplates',
          'DIRS': [],
          'APP_DIRS': True,
          'OPTIONS': {
              'context_processors': [
                  'django.template.context_processors.debug',
                  'django.template.context_processors.request',
                  'django.contrib.auth.context_processors.auth',
                  'django.contrib.messages.context_processors.messages',
              ],
          },
      },
  ]
  
  WSGI_APPLICATION = 'chhichhi_project.wsgi.application'
  
  
  # Database
  # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
  
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'cicidb1',
          'USER': 'postgres',
          'PASSWORD': '1324pass',
          'HOST': 'localhost',
          'PORT': '5432',
      }
  }
  
  
  # Password validation
  # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
  
  AUTH_PASSWORD_VALIDATORS = [
      {
          'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
      },
  ]
  
  
  # Internationalization
  # https://docs.djangoproject.com/en/2.2/topics/i18n/
  
  LANGUAGE_CODE = 'en-us'
  
  TIME_ZONE = 'UTC'
  
  USE_I18N = True
  
  USE_L10N = True
  
  USE_TZ = True
  
  
  # Static files (CSS, JavaScript, Images)
  # https://docs.djangoproject.com/en/2.2/howto/static-files/
  
  STATICFILES_DIRS = [
      os.path.join(BASE_DIR, 'blog/static/')
  ]
  
  STATIC_ROOT= os.path.join(BASE_DIR, 'static')
  STATIC_URL = '/static/'
  
  MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  MEDIA_URL = '/media/'
  
  CRISPY_TEMPLATE_PACK = 'bootstrap4'
  
  LOGIN_REDIRECT_URL = 'blog_home'
  LOGIN_URL = 'login'
  
  
  EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  # my custom local host mail server
  EMAIL_HOST = 'smtp.gmail.com'
  EMAIL_PORT = 587
  EMAIL_USE_TLS = True
  EMAIL_HOST_USER = 'youremail@gmail.com'
  EMAIL_HOST_PASSWORD = 'yourpassword'
  # my custom local host mail server
  # EMAIL_HOST = 'smtp.gmail.com'
  # EMAIL_PORT =  465
  # EMAIL_HOST_USER = 'youremail@gmail.com'
  # EMAIL_HOST_PASSWORD = 'yourpassword'
  # EMAIL_USE_TLS = False
  # EMAIL_USE_SSL = True
  
  
  # END custom mail server
  </code></pre>
  
  <p><strong>This is views.py file</strong></p>
  
  <pre><code>from django.shortcuts import render, redirect
  from django.contrib import messages
  from django.contrib.auth.decorators import login_required
  
  # Email verification
  from django.http import HttpResponse
  # from django.shortcuts import render, redirect
  from django.contrib.auth import login, authenticate
  # from .forms import UserSignUpForm
  from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
  from django.contrib.sites.shortcuts import get_current_site
  from django.utils.encoding import force_bytes, force_text
  from django.utils.http import urlsafe_base64_encode, 
  urlsafe_base64_decode
  from django.template.loader import render_to_string
  from .token_generator import account_activation_token
  from django.contrib.auth.models import User
  from django.core.mail import EmailMessage
  # Email verificatin end
  
  
  def register(request):
      if request.method == 'POST':
          form = UserRegisterForm(request.POST)
          if form.is_valid():
              user = form.save(commit=False)
              user.is_active = False
              user.save()
              current_site = get_current_site(request)
              email_subject = 'Activate Your Account'
              message = render_to_string('activate_account.html', {
                  'user': user,
                  'domain': current_site.domain,
                  'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
                  'token': account_activation_token.make_token(user),
              })
              to_email = form.cleaned_data.get('email')
              email = EmailMessage(email_subject, message, to=[to_email])
              email.send()
              return HttpResponse('We have sent you an email, please confirm your email address to complete registration')
              # username = form.cleaned_data.get('username')
              # messages.success(request, f'Your account has been created! You are now able to log in.')
              # return redirect('login')
      else:
          form = UserRegisterForm()
      return render(request, 'users/register.html',{'form': form})
  
  
  def activate_account(request, uidb64, token):
      try:
          uid = force_bytes(urlsafe_base64_decode(uidb64))
          user = User.objects.get(pk=uid)
      except(TypeError, ValueError, OverflowError, User.DoesNotExist):
          user = None
      if user is not None and account_activation_token.check_token(user, token):
          user.is_active = True
          user.save()
          login(request, user)
          return HttpResponse('Your account has been activate successfully')
      else:
          return HttpResponse('Activation link is invalid!')
  </code></pre>
  
  <p><strong>This is urls.py file</strong></p>
  
  <pre><code>from django.conf.urls import url
  from . import views
  
  
  urlpatterns = [
      url(r'^$', views.register, name='register_user'),
      url(r'^activate/(?P&lt;uidb64&gt;[0-9A-Za-z_\-]+)/(?P&lt;token&gt;[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
          views.activate_account, name='activate'),
  ]
  </code></pre>
  
  <p><strong>This is token_generator.py file</strong></p>
  
  <pre><code>from django.contrib.auth.tokens import PasswordResetTokenGenerator
  from django.utils import six
  
  
  class TokenGenerator(PasswordResetTokenGenerator):
      def _make_hash_value(self, user, timestamp):
          return (
              six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
          )
  
  
  account_activation_token = TokenGenerator()
  </code></pre>
  
  <p><strong>This is main urls.py file</strong></p>
  
  <pre><code>from django.contrib import admin
  from django.contrib.auth import views as auth_views
  from django.urls import path, include
  from django.conf import settings
  from django.conf.urls.static import static
  from users import views as user_views
  
  urlpatterns = [
      path('admin/', admin.site.urls),
      # path('register/', user_views.register, name='register'),
      path('register/', include('users.urls')), # new url
      path('profile/', user_views.profile, name='profile'),
      path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name='login'),
      path('logout/', auth_views.LogoutView.as_view(template_name = 'users/logout.html'), name='logout'),
      path('password-reset/', 
              auth_views.PasswordResetView.as_view(template_name = 'users/password_reset.html'), 
              name='password_reset'),
      path('password-reset/done/', 
              auth_views.PasswordResetDoneView.as_view(template_name = 'users/password_reset_done.html'), 
              name='password_reset_done'),
      path('password-reset-confirm/&lt;uidb64&gt;/&lt;token&gt;/', 
              auth_views.PasswordResetConfirmView.as_view(template_name = 'users/password_reset_confirm.html'), 
              name='password_reset_confirm'),
      path('password-reset-complete/', 
                  auth_views.PasswordResetCompleteView.as_view(template_name = 'users/password_reset_complete.html'), 
                  name='password_reset_complete'),
      path('', include('blog.urls')),
  
  ]
  
  if settings.DEBUG:
      urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
      urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  </code></pre>
  
  <p><strong>This is the register.html file</strong></p>
  
  <pre><code>{% extends 'blog/base.html' %}
  {% load crispy_forms_tags %}
  {% block title %}Registration{% endblock title %}
  
  {% block content %}
      &lt;div class="content-section"&gt;
          &lt;form method="POST"&gt;
              {% csrf_token %}
              &lt;fieldset class="form-group"&gt;
                   &lt;legend class="border-bottom mb-4"&gt;Join Today&lt;/legend&gt;
                   {{ form|crispy }}
              &lt;/fieldset&gt;
              &lt;div class="form-group"&gt;
                  &lt;button class="btn btn-outline-info" type="submit"&gt;Sign Up&lt;/button&gt;
              &lt;/div&gt;
          &lt;/form&gt;
          &lt;div class="border-top pt-3"&gt;
              &lt;small class="text-muted"&gt;
                  Already Have An Account? &lt;a class="ml-2" href="{% url 'login' %}"&gt;Sign In&lt;/a&gt;
              &lt;/small&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  {% endblock content %}
  </code></pre>