8

I would like to create/add new users to my app in Django using the user input. I am using the default login provided by django. I am trying to add users to the default login. The example in https://docs.djangoproject.com/en/dev/topics/auth/:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'john password')

passes the username and password. But i would like this to happen with user input. How do i do this?

Need some guidance...

I tried this but it doesn't seem to work:

def lexusadduser(request):
    """
    add user
    """
    if request.method == "POST":
        user.save()

        auth_user = authenticate(username=user.username,password=user.password)

        if auth_user is not None:
            lexusmain(request)
        else:
            return render('adduser.html') 
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • This example has obvious errors. What logic do you want to achieve with this view? – jdi Jul 02 '12 at 04:07
  • I am trying to add a new user and if added send him to main page else send him back to add user page... – lakshmen Jul 02 '12 at 04:09
  • I understand that part, but what are you trying to accomplish with the authentication? You are saving a `user` object that does not exist, and then you are authenticating that non-existant variable. – jdi Jul 02 '12 at 04:11
  • I understand what the problem is... But how do i get the user input and use it to add a new user and if added send him to main page else send him back to add user page.. Need some help... – lakshmen Jul 02 '12 at 04:22

2 Answers2

28

First thing you need to do is create a ModelForm:

forms.py

from django.contrib.auth.models import User

class UserForm(ModelForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password')

A ModelForm automatically builds your form off a model you provide. It handles the validations based on the fields.

views.py

from forms import UserForm
from django.contrib.auth import login
from django.http import HttpResponseRedirect

def lexusadduser(request):
    if request.method == "POST":
        form = UserForm(request.POST)
        if form.is_valid():
            new_user = User.objects.create_user(**form.cleaned_data)
            login(new_user)
            # redirect, or however you want to get to the main view
            return HttpResponseRedirect('main.html')
    else:
        form = UserForm() 

    return render(request, 'adduser.html', {'form': form}) 

If its a POST request, we create a UserForm from the POST values. Then we check if its a valid form. If it is, we create the user, otherwise we return the form with the errors. If its not a POST request, we send a clean form

template

<form method="post" action="">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Create new user account" />
</form>
steveha
  • 74,789
  • 21
  • 92
  • 117
jdi
  • 90,542
  • 19
  • 167
  • 203
  • This answer was useful to me, but it is incomplete. The form template should include the CSRF token code and a submit button. I'm going to just go ahead and edit them in. Note that this form won't be pretty by default, but will work. Making it look good isn't in the scope of the question. – steveha Apr 02 '14 at 03:19
  • More info available here: http://www.tangowithdjango.com/book/chapters/login.html – steveha Apr 02 '14 at 03:36
0

Use a form. Air coding here:

class SignupForm(forms.Form):
    username = forms.CharField()
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput)
Naddiseo
  • 1,014
  • 1
  • 7
  • 17
  • this doesn't add user to the default right.this is like creating a new model right? if i am wrong, correct me... – lakshmen Jul 02 '12 at 04:03
  • @lakesh: Naddiseo is suggesting to use a form within your view, but this answer is extremely vague so I am not surprised that you aren't clear on it. Its not a new model. – jdi Jul 02 '12 at 04:08