Я пытаюсь создать представление, которое обрабатывает подтверждение по электронной почте, но я продолжаю получать сообщение об ошибке, ваша помощь очень важна
My views.py
import re
from django.contrib.auth import login, logout, authenticate
from django.shortcuts import render, HttpResponseRedirect, Http404
# Create your views here.
from .forms import LoginForm, RegistrationForm
from . models import EmailConfirmation
SHA1_RE = re.compile('^[a-f0-9]{40}s')
def activation_view(request, activation_key):
if SHA1_RE.search(activation_key):
print('activation is real')
try:
instance = EmailConfirmation.objects.get(activation_key=activation_key)
except EmailConfirmation.DoesNotExist:
instance = None
raise Http404
if instance is not None and not instance.confirmed:
print('Confirmation complete')
instance.confirmed = True
instance.save()
elif instance is not None and instance.confirmed:
print('User already confirmed')
else:
pass
context = {}
return render(request, "accounts/activation_complete.html", context)
else:
pass
urls.py
from django.urls import path
from .views import(
logout_view,
login_view,
register_view,
activation_view,
)
urlpatterns = [
path('accounts/logout/', logout_view, name='auth_logout'),
path('accounts/login/', login_view, name='auth_login'),
path('accounts/register/', register_view, name='auth_register'),
path('accounts/activate/<activation_key>/', activation_view, name='activation_view'),
]
models.py
class EmailConfirmation(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
activation_key = models.CharField(max_length=200)
confirmed = models.BooleanField(default=False)
def __str__(self):
return str(self.confirmed)
def activate_user_email(self):
#send email here
activation_url = 'http://localhost:8000/accounts/activate%s' %(self.activation_key)
context = {
'activation_key': self.activation_key,
'activation_url': activation_url,
'user': self.user.username
}
message = render_to_string('accounts/activation/actiavtion_message.txt', context)
subject = 'Email actiavtion key'
print(message)
self.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
def email_user(self, subject, message, from_email=None, **kwargs):
send_mail(subject, message, from_email, [self.user.email], kwargs)
ОШИБКА
ValueError at /accounts/activate/d99e89141eaf2fa786a3b5215ab4aa986e411bcc/
The view accounts.views.activation_view didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/activate/d99e89141eaf2fa786a3b5215ab4aa986e411bcc/
Django Version: 3.0.6
Exception Type: ValueError
Exception Value:
The view accounts.views.activation_view didn't return an HttpResponse object. It returned None instead.
Exception Location: C:\Users\Martin\Desktop\My Projects\Savanna\venv\lib\site-packages\django\core\handlers\base.py in _get_response, line 124
Python Executable: C:\Users\Martin\Desktop\My Projects\Savanna\venv\Scripts\python.exe
Python Version: 3.8.2
Python Path:
['C:\\Users\\Martin\\Desktop\\My Projects\\Savanna',
'C:\\Users\\Martin\\Desktop\\My '
'Projects\\Savanna\\venv\\Scripts\\python38.zip',
'c:\\program files\\python38\\DLLs',
'c:\\program files\\python38\\lib',
'c:\\program files\\python38',
'C:\\Users\\Martin\\Desktop\\My Projects\\Savanna\\venv',
'C:\\Users\\Martin\\Desktop\\My Projects\\Savanna\\venv\\lib\\site-packages']
Server time: Thu, 28 May 2020 09:05:51 +0000