在Django中重置密码

时间:2015-06-07 04:33:41

标签: python django

我查看了本教程https://www.youtube.com/watch?v=z6pXNf2SzQQ,解释了如何将邮件发送到重置密码,我已按照所有步骤操作但总是遇到同样的错误:没有名为'my_app.views.django'的模块; 'my_app.views'不是一个包。对于这种情况my_app = melomanos。我在我的网站模板文件夹中有Django重置密码的所有模板。

完整的网站根目录是:

root of my app

显示的错误是:我知道我有一个配置错误的网址,但我不明白我是如何正确配置的。谢谢你的合作。

ImportError at /resetpassword/
No module named 'melomanos.views.django'; 'melomanos.views' is not a package
Request Method: GET
Request URL:    http://localhost/resetpassword/
Django Version: 1.8.2
Exception Type: ImportError
Exception Value:    
No module named 'melomanos.views.django'; 'melomanos.views' is not a package
Exception Location: C:\Python34\lib\importlib\__init__.py in import_module, line 109
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.3
Python Path:['c:\\labsoft',
'C:\\Python34\\lib\\site-packages\\psycopg2-2.6-py3.4-win-amd64.egg',
'C:\\Windows\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages',
'labsoft/melomanos',
'/melomanos']

这是代码:

melomanos \ urls.py

from django.conf.urls import patterns, url, include
from django.views.generic.base import TemplateView
from django.contrib import admin
from .views import Buscar_view
admin.autodiscover()

urlpatterns = patterns('melomanos.views',
                   url(r'^admin/', include(admin.site.urls)),
                   url(r'^$','trabajos_all_view',name='url_index'),
                   url(r'^register/$','register_view',name='vista_registro'),
                   url(r'^login/$','login_view',name='vista_login'),
                   url(r'^logout/$','logout_view',name='vista_logout'),
                   url(r'^perfil/$','registro_view',name='vista_perfil'),
                   url(r'^publicar/$','trabajomusical_view', name='vista_publicar'),
                   url(r'^trabajos/$','trabajos_view',name='vista_trabajos'),
                   url(r'^trabajo/(?P<id_trabajo>.*)/$','solo_trabajo_view', name='vista_trabajo'),
                   url(r'^buscar/$',Buscar_view.as_view(),name='vista_buscar'),
                   url('', include('django.contrib.auth.urls')),
                   url(r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done', name='password_reset_done'),
                   url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
                   url(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>,+)/$', 'django.contrib.auth.views.password_reset_confirm'),
                   url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
                   )

登录页面上的链接:

<p>Forgot your password?<a href="/resetpassword/">Reset Password</a></p>

settings.py

    INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'melomanos',
    )

2 个答案:

答案 0 :(得分:4)

melomanos\urls.py上使用网址前缀。

urlpatterns = patterns('melomanos.views',
    url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
)

因此/resetpassword/正在调用melomanos.views.django.contrib.auth.views.password_reset而不是django.contrib.auth.views.password_reset

对于重置密码视图,删除前缀将解决此问题。

您可以从前缀块中删除resetpassword,然后添加前缀

urlpatterns += patterns('',
    url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
)

答案 1 :(得分:4)

TL; DR; - 不要将视图引用为字符串 - 导入相关模块并传递视图函数。在这种情况下,您不需要使用prefix

一些历史记录:

很久以前,Django发布了url函数,您可以在其中定义所需视图的路径,并传递字符串。有很多具有相同前缀的视图,这就是为什么Django团队还在prefix函数中提供django.conf.urls.patterns()参数,它允许您提取视图路径的相互部分。

今天,这只不过是遗物了。

现在您应该在urls.py模块中导入视图并直接使用函数。 prefix论证没有其他理由。

此外,由于Django 1.8 {@ 1}}函数已弃用,将在Django 2.0中删除:

patterns

相反,您应该只传递一个模式列表(docs):

def patterns(prefix, *args):
    warnings.warn(
        'django.conf.urls.patterns() is deprecated and will be removed in '
        'Django 2.0. Update your urlpatterns to be a list of '
        'django.conf.urls.url() instances instead.',
        RemovedInDjango20Warning, stacklevel=2
    )
    # Implementation