Jeremy
Jeremy

Reputation: 2546

Django Creating User Account Manually

Django has a built in admin page where it comes with a feature to add/edit/remove user (and its authentication).

However, i need to create a custom form involving the following models

employee/models.py

# Stores profile details such as DoB, Martial Status, TFN and so on
class Profile(models.Model):
    user = models.OneToOneField(User)

    MARTIAL_STATUS = (
        ('s', 'Single'),
        ('m', 'Married'),
        ('d', 'Divorced'),
        ('w', 'Widowed')
    )
    martial = models.CharField(max_length=1, choices=MARTIAL_STATUS, null=True)
    tfn = models.CharField(max_length=200, blank=False)

What i want to do is to have one form where user can enter information about the username, first_name, and so on along with all fields required in my models.

So far this is what i have donedjango-form

Notice how an account needs to be created first, before additional information (from different model) can be inserted

ps: i am using Django ver 1.6

Upvotes: 1

Views: 5038

Answers (3)

ruddra
ruddra

Reputation: 52028

If I understand correctly, you need to create custom users for adding it to your profile form in admin site. Why don't you use django shell? For example:

where manage.py resides, open terminal/command prompt and type:

>>python manage.py shell

In [1]: from django.contrib.auth.models import User

In [2]: i=User(username="test")

In [3]: i.save()

In [4]: i.set_password('test')

In [5]: i.save()

You can use this username/password to login into site.

EDIT:

Assuming your admin url is like www.mysite.com/admin, you can access user directly using this like: www.mysite.com/admin/auth/user/add/. Also admin interface looks like this: enter image description here.

And if you want to add email address and other data, you can press save and continue editing like below: enter image description here

This will lead you to updating user contents.enter image description here

If you want to create user not from admin site, then less painful way to implement user registration is using UserCreationForm.

from django.views.generic.edit import CreateView
from django.contrib.auth.forms import UserCreationForm

urlpatterns = patterns('',
    url('^register/', CreateView.as_view(
            template_name='register.html',
            form_class=UserCreationForm,
            success_url='/'
    )),
    url('^accounts/', include('django.contrib.auth.urls')),

    # rest of your URLs as normal
)

you have to create a register.html here though like:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Create" />
</form>

details: http://www.obeythetestinggoat.com/using-the-built-in-views-and-forms-for-new-user-registration-in-django.html

Upvotes: 4

Nava
Nava

Reputation: 6586

There should be + symbol near by user model dropdown box which eases the user to quick add, like the below one.

enter image description here

If the user model in registered with admin i think you could get access to create it on the fly.

OR

As Ruddra's above answer, you have create all user profiles, then you can fillup the form in straight manner.

Upvotes: 0

samidarko
samidarko

Reputation: 632

I usually start from the documentation full example then I adapt it for my needs. You keep the Django permissions but you also add some custom permissions.

I don't know how far are you in your project but if you're just starting I recommend you to use twoscoops templates or cookiecutter-django of Daniel Greenfield. It also implements django all auth library which is very useful for social authentifcations.

Upvotes: 0

Related Questions