bne bne - 7 months ago 31
Python Question

attributeerror: 'module' object has no attribute 'register_success'

I had a project with log application . I removed it and have done a registration application in order to integrate Database and if you are not signed up you can't sign in ..... But When I runserver my Django project . I obtain an attribute error in reg application but I didn't known how to solve it .

reg/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.login, name='login'),
url(r'^$', views.auth_view, name='auth_view'),
url(r'^$', views.loggedin, name='loggedin'),
url(r'^$', views.invalid_login, name='invalid_login'),
url(r'^$', views.register_user, name='register_user'),
url(r'^$', views.logout, name='logout'),
url(r'^$', views.register_success, name='register_success'),

]


reg/views.py

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
def login(request):
c = {}
c.update(csrf(request))
return render_to_response('login.html', c)

def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticated(username=username, password=password)

if user is not None:
auth.login(request, user)
return HttpResponseRedirect('loggedin')
else:
return HttpResponseRedirect('invalid')

def loggedin(request):
return render_to_response('loggedin.html',
{'full_name': request.user.username})
def invalid_login(request):
return render_to_response('invalid_login.html')
def logout(request):
auth.logout(request)
return render_to_response('logout.html')
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('register_success')


args = {}
args.update(csrf(request))

args['form'] = UserCreationForm()

return render_to_response('register.html', args)
def register_success(request):
return render_to_response('register_success.html')


site/urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.contrib.auth import views
from log.forms import LoginForm

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^aps/', include('aps.urls')),
url(r'^mail/', include('mail.urls')),
url(r'^reg/', include('reg.urls')),
url(r'^reg/register_success/$', views.register_success),
url(r'^reg/register/$', views.register_user),
url(r'^reg/invalid/$', views.invalid_login),
url(r'^reg/loggedin/$', views.loggedin),
url(r'reg/login/$', views.login),
url(r'reg/logout/$', views.logout),
url(r'reg/auth/$', views.auth_view),


]

Answer

You don't need to add all urls under reg/urls.py individually into site/urls.py as url(r'^reg/', include('reg.urls')) already does that automatically for you.

Here is the corrected version of urlpatterns defined in sites/urls.py:

from django.conf.urls import patterns

urlpatterns = patterns(
    "",
    (r'^admin/', admin.site.urls),    
    (r'^aps/', include('aps.urls'),
    (r'^mail/', include('mail.urls'),
    (r'^reg/', include('reg.urls'),
)

and reg/urls.py:

from django.conf.urls import patterns

urlpatterns = patterns(
    "",
    url(r'^login/$', views.login, name='login'),
    url(r'^auth/$', views.auth_view, name='auth_view'),
    url(r'^loggedin/$', views.loggedin, name='loggedin'),
    url(r'^invalid/$', views.invalid_login, name='invalid_login'),
    url(r'^register/$', views.register_user, name='register_user'),
    url(r'^logout/$', views.logout, name='logout'),
    url(r'^register_success/$', views.register_success, name='register_success'),
)