T
T
trytrytry2013-12-17 16:31:52
Django
trytrytry, 2013-12-17 16:31:52

Problems writing a logging form?

I am writing an educational website. I wrote a logging page, everything is displayed correctly, but as soon as I click ok, it goes to the wrong link, generally goes to a link I don't understand.
Here is my views.py:

def index(request):

    if request.user.is_authenticated():
        
        return render(request, 'account/index.html',
                      {
                       'user' : request.user,
                       
                       }
                      )
    else:
        return HttpResponseRedirect(reverse('account.views.login'))
   

def logout(request):
    if request.user.is_authenticated():
        auth.logout(request)
    return HttpResponseRedirect(reverse('account.views.login'))


def login(request):
    if request.method == 'POST':
        f = LoginForm(request.POST)
        if f.is_valid():
            username = f.cleaned_data['username']
            password = f.cleaned_data['password']
            user = auth.authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect(reverse('account.views.index'))
    else:
        f = LoginForm()

    return render(request, 'account/login.html', {'f': f})

and here is the urls.py
urlpatterns = patterns('',
    url(r'^$', 'account.views.index', name='index'),
    
    url(r'^login/$', 'account.views.login', name = 'login'),
    
)

and another urls.py
urlpatterns = patterns('',
    
    url(r'^admin/', include(admin.site.urls)),
    url(r'^account/', include('account.urls')),
)

If logging is successful, it goes to index.html where the username is displayed, but for some reason it goes to this page:
http://127.0.0.1:8000/login/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maxaon, 2013-12-17
@trytrytry

The error is here:
The "reverse" function searches by name, respectively, it should be like this:
Similarly
reverse('account.views.index')

T
trytrytry, 2013-12-17
@trytrytry

first on the page .../account/login the form is displayed correctly, but then to just go to .../account/ it goes to .../login/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question