首页 > 解决方案 > AttributeError:模块“collection.views”没有属性“create_word”

问题描述

我正在尝试为允许用户为字典创建自己的单词的表单创建路由。

AttributeError: module 'collection.views' has no attribute 'create_word'每次尝试运行本地服务器时,我都会收到以下错误

错误提醒注意 urls.py 中的这行代码

path('accounts/create_word/', views.create_word, name='registration_create_word'),

不确定是什么问题,因为我已经定义了位于 views.py 中的 create_word 方法

def create_word(request):
        form_class = WordForm
        # if users are coming from a submitted form, do this
        if request.method == 'POST':
            #grab the data from the submitted form and apply it to the form
            form = form_class(request.POST)
            if form.is_valid():
                #create an instance but don't save
                word = form.save(commit=False)
                #set additional details
                word.user = request.user
                user.slug = slugify(word.name)
                #save the object
                word.save()
                # redirect to newly created word object
                return redirect('word_detail', slug=word.slug)
            #otherwise create the form
            else:
                form = form_class()

            return render(request, 'words/create_word.html', {
                'form': form,
            })

我也相信我正确导入了集合视图

from collection.backends import MyRegistrationView
from collection import views

非常感谢澄清我错过了什么或做错了什么

标签: pythondjangopython-3.xattributeerrordjango-registration

解决方案


推荐阅读