Reputation: 2546
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
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
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.
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>
Upvotes: 4
Reputation: 6586
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.
As Ruddra's above answer, you have create all user profiles, then you can fillup the form in straight manner.
Upvotes: 0
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