在Django中重置密码

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

The complete site root is:

root of my app

The error that shows is: 我知道我有一个配置错误的网址,但我不明白我是如何正确配置的 . 谢谢你的合作 .

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'),
                   )

The link at the login page:

<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)

2 years ago

melomanos\urls.py 上您使用的是url前缀 .

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"),
)

2 years ago

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

一些历史记录:

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

今天,这不过是遗物 .

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

此外,由于Django 1.8, patterns 函数已弃用,将在Django 2.0中删除:

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

相反,你应该像这样传递你的模式列表(docs):

from django.conf.urls import url
from myapp import views

urlpatterns = [
    url('^$', views.myview),
    url('^other/$', views.otherview),
]