Is there a example in django 1.6 and python 3 to build a accounts app (include:register , login , and logout )
Question: I do have read the offical document, but it describes every facility
separately, after reading 'User authentication in Django' ,'First steps' ,
'The model layer', 'The view layer' and 'The template layer' and 'Forms' , I
still donot know how to create a account system.

there seems no django 1.6 and python 3 built account app source code or
tutorial. where can I get them, thanks.

update:

All I what is a account app which I can plug it into any new project. Its urls
will look like this:

accounts/register (the form class of this page is created from the class User
in django.contrib.auth)

accounts/login

accounts/logout

accounts/profile (the form class of this page is created from the model which
has a field `OneToOneField(User)`)


Answer: In your views.py

    
    
    from django.http import HttpResponse, HttpResponseRedirect
    from django.contrib.auth import authenticate, login, logout
    from django.core.context_processors import csrf
    
    #Import a user registration form
    from YourApp.forms import UserRegisterForm
    
    # User Login View
    def user_login(request):
        if request.user.is_anonymous():
            if request.method == 'POST':
                username = request.POST['username']
                password = request.POST['password']
                #This authenticates the user
                user = authenticate(username=username, password=password)
                if user is not None:
                    if user.is_active:
                        #This logs him in
                        login(request, user)
                    else:
                        return HttpResponse("Not active")
                else:
                    return HttpResponse("Wrong username/password")
        return HttpResponseRedirect("/")
    
    # User Logout View
    def user_logout(request):
        logout(request)
        return HttpResponseRedirect('/')
    
    # User Register View
    def user_register(request):
        if request.user.is_anonymous():
            if request.method == 'POST':
                form = UserRegisterForm(request.POST)
                if form.is_valid:
                    form.save()
                    return HttpResponse('User created succcessfully.')
            else:
                form = UserRegisterForm()
            context = {}
            context.update(csrf(request))
            context['form'] = form
            #Pass the context to a template
            return render_to_response('register.html', context)
        else:
            return HttpResponseRedirect('/')
    

In your forms.py

    
    
    from django import forms
    from django.contrib.auth.models import User
    from django.contrib.auth.forms import UserCreationForm
    
    class UserRegisterForm(UserCreationForm):
    
        class Meta:
            model = User
            fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
    

In your urls.py:

    
    
    # Accounts urls
    url(r'accounts/login/$', 'YourApp.views.user_login'),
    url(r'accounts/logout/$', 'YourApp.views.user_logout'),
    url(r'accounts/register/$', 'YourApp.views.user_register'),
    

At last, in register.html:

    
    
    <form action="/accounts/register/" method="POST"> {% csrf_token %}
    <h2>Please enter your details . . .</h2>
        {{ form.as_p }}
    <input type="submit" value="Sign Up">
    </form>
    

Hope this helps.

