简体   繁体   中英

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 done Django形式

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

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: 在此处输入图片说明 .

And if you want to add email address and other data, you can press save and continue editing like below: 在此处输入图片说明

This will lead you to updating user contents. 在此处输入图片说明

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

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.

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

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM