python - 模块“accounts.views”没有属性“signup”
问题描述
File "/Users/work/Projects/blog/myblog/urls.py", line 22, in <module>
path('post/<int:pk>/signup/', views.signup, name="signup"),
AttributeError: module 'accounts.views' has no attribute 'signup'
我的博客/views.py
import json
from urllib import request
from django.views import View
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Post, Comment
from .forms import CommentForm
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMessage
from django.http import HttpResponse
from django.shortcuts import render
from django.template.loader import render_to_string
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
UserModel = get_user_model()
from .forms import SignUpForm
from .tokens import account_activation_token
def signup(request):
if request.method == 'GET':
return render(request, 'signup.html')
if request.method == 'POST':
form = SignUpForm(request.POST)
# print(form.errors.as_data())
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activate your account.'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
form = SignUpForm()
return render(request, 'signup.html', {'form': form})
def activate(request, uidb64, token):
try:
uid = urlsafe_base64_decode(uidb64).decode()
user = UserModel._default_manager.get(pk=uid)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and default_token_generator.check_token(user, token):
user.is_active = True
user.save()
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
class BlogListView(ListView):
model = Post
template_name = 'home.html'
context_object_name = 'posts'
paginate_by = 2
queryset = Post.objects.all()
class BlogDetailView(DetailView):
model = Post
template_name = 'post_detail.html'
class BlogCreateView(CreateView):
model = Post
template_name = 'post_new.html'
fields = ['title', 'author', 'body', 'header_image']
class BlogCommentView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'post_comment.html'
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
return super().form_valid(form)
success_url = reverse_lazy('home')
#fields = '__all__'
class BlogUpdateView(UpdateView):
model = Post
template_name = 'post_edit.html'
fields = ['title', 'body', 'header_image']
class BlogDeleteView(DeleteView):
model = Post
template_name = 'post_delete.html'
success_url = reverse_lazy('home')
@property
def image_url(self):
if self.image:
return getattr(self.photo, 'url', None)
return None
帐户/views.py
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic
class SignUpView(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
我的博客/urls.py
from django.urls import path
from accounts import views
from .views import (
BlogListView,
BlogDetailView,
BlogCreateView,
BlogUpdateView,
BlogDeleteView,
BlogCommentView,
)
urlpatterns = [
path('post/new/', BlogCreateView.as_view(), name='post_new'),
path('post/<int:pk>/', BlogDetailView.as_view(), name='post_detail'),
path('post/<int:pk>/edit/', BlogUpdateView.as_view(), name='post_edit'),
path('post/<int:pk>/delete/', BlogDeleteView.as_view(), name='post_delete'),
path('post/<int:pk>/comment/', BlogCommentView.as_view(), name='post_comment'),
path('', BlogListView.as_view(), name='home'),
path('post/<int:pk>/signup/', views.signup, name="signup"),
path('activate/<uidb64>/<token>/', views.activate, name='activate'),
]
博客/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('accounts.urls')),
path('', include('myblog.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
表格.py
from django import forms
from .models import Post, Comment
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'body')
widgets = {
'name': forms.TextInput(attrs={'class': 'form-control'}),
'body': forms.Textarea(attrs={'class': 'form-control'}),
}
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'username')
由于我是 django 新手,可能是我在错误的地方插入了一些代码
我希望问题尽可能清楚以解决问题,但我不知道该怎么做,因为我不了解代码中的所有内容,所以我请你写任何建议,如果你需要扔别的东西,告诉我,我会把它扔掉
ps user_registration = 我的博客
帐户= 我的博客
我的accounts/views.py - 它是一个包含迁移和文件的单独文件夹
我不知道,也许指南中的views.py代码不需要插入到myblog/views.py中,而是插入到accounts.py中,因为“注册”和“活动”在myblog/urls.py和出现一条消息,您需要在accounts/view.py中创建这些功能,总的来说,我很困惑,这可能是一个无用的问题,如果一切都太难了,请把我指向另一个方向,我该如何尝试通过邮件注册,mb获取一些视频教程或其他指南,非常感谢
解决方案
尝试将以下代码添加到您的 myblog/urls.py 文件中
from .views import signup
推荐阅读
- java - 以下 switch 语句有什么问题?
- meshlab - 默认情况下,Meshlab 如何在导出网格中计算法线?
- javascript - 没有 Node.js 我怎么能做 tweetnacl.sealedbox.seal?
- node.js - waitForSelector 和 querySelectorAll 与 puppeteer
- windows - 如何加速这个 Bash mv 脚本?
- mysql - MariaDB 无法更改/禁用 sql 模式
- php - 在 AcceptSuite\create-an-accept-payment-transaction.php 中找不到类 'net\authorize\api\contract\v1\MerchantAuthenticationType'
- javascript - React Native 中的可编辑文本
- ios - 从 TestFlight 安装后,iOS 应用程序在启动时崩溃
- python - 使用 spotipy 提取艺术家流派和歌曲发布日期