【问题标题】:The view accounts.views.activation_view didn't return an HttpResponse object. It returned None instead视图 accounts.views.activation_view 没有返回 HttpResponse 对象。它返回 None 而不是
【发布时间】:2025-12-27 20:50:16
【问题描述】:

我正在尝试创建一个处理电子邮件确认的视图,但我不断收到错误,非常感谢您的帮助

我的意见.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

【问题讨论】:

    标签: python django hashlib


    【解决方案1】:

    这意味着您没有在 http 响应对象之前添加 return 即您可能忘记了在渲染方法之前返回。我猜是因为 else: pass 你得到了这个错误。那么,删除 else:pass 部分,为什么你的上下文字典是空的?

    【讨论】:

      【解决方案2】:

      在这一行内:

      SHA1_RE = re.compile('^[a-f0-9]{40}s')
      

      用 $ 代替 s

      【讨论】: