Hi every one
I am using django-registration application which is working fine with
fields(username,password,email,first_name,last_name).
I wanted to add some more fields like age,phone while registration
time or i may create seperate profile with that particular user.
models.py
-------------
class ProfileManager(models.Manager):
def profile_callback(self, user):
new_profile = UserProfile.objects.create
(user=user,age=age,first_name=first_name,last_name=last_name)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
gender = models.CharField(_('gender'), max_length=1,
choices=GENDER_CHOICES)
age = models.IntegerField(_('age'), max_length=3, unique=True,
help_text=_("Numeric characters only"))
first_name = models.CharField(_('first name'), max_length=30,
blank=True)
last_name = models.CharField(_('last name'), max_length=30,
blank=True)
objects = ProfileManager()
def __unicode__(self):
return u"Registration information for %s" % self.user
def get_absolute_url(self):
return ('profiles_profile_detail', (), { 'username':
self.user.username })
forms.py
-----------
from django.contrib.auth.models import User
from foodies.foodapp.models import UserProfile
attrs_dict = { 'class': 'required' }
class ProfileForm(forms.Form):
"""
Subclasses should feel free to add any additional validation
they need, but should either preserve the base ``save()`` or
implement
a ``save()`` which accepts the ``profile_callback`` keyword
argument and passes it through to
``RegistrationProfile.objects.create_inactive_user()``.
"""
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput
(attrs=attrs_dict),
label=_(u'username'))
#gender = forms.CharField(label = _('Gender'), widget =
forms.Select(choices=UserProfile.GENDER_CHOICES))
age = forms.EmailField(widget=forms.TextInput(attrs=dict
(attrs_dict,
maxlength=75)),
label=_(u'email address'))
first_name = forms.CharField(widget=forms.PasswordInput
(attrs=attrs_dict, render_value=False),
label=_(u'password'))
last_name = forms.CharField(widget=forms.PasswordInput
(attrs=attrs_dict, render_value=False),
label=_(u'password (again)'))
def clean_username(self):
""" Validate that the username is alphanumeric and is not
already in use. """
try:
user = User.objects.get(username__iexact=self.cleaned_data
['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already
taken. Please choose another.'))
def save(self, user):
user_obj = User.objects.get(pk=user.id)
user_obj.first_name = self.cleaned_data['first_name']
user_obj.last_name = self.cleaned_data['last_name']
try:
profile_obj = user.get_profile()
except ObjectDoesNotExist:
profile_obj = UserProfile()
profile_obj.user = user
profile_obj.birthdate = self.cleaned_data['birthdate']
profile_obj.gender = self.cleaned_data['gender']
profile_obj.save()
user_obj.save()
def create_profile(request, form_class=ProfileForm, success_url=None,
template_name='registration/profile.html',
extra_context=None,profile_callback=None,):
print " I am inside profile"
try:
profile_obj = request.user.get_profile()
print "Profile obj : ", form_class
return HttpResponseRedirect(reverse('profiles_edit_profile'))
except ObjectDoesNotExist:
pass
'''if success_url is None:
success_url = reverse('profiles_profile_detail',
kwargs={ 'username':
request.user.username })'''
'''if form_class is None:
form_class = utils.get_profile_form()'''
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid():
profile_obj = form.save(commit=False)
profile_obj.user = request.user
profile_obj.save()
if hasattr(form, 'save_m2m'):
form.save_m2m()
return HttpResponseRedirect(success_url)
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{ 'form': form },
context_instance=context)
create_profile = login_required(create_profile)
urls.py
--------
from foodies.foodapp.regview import create_profile
#admin.autodiscover()
urlpatterns = patterns('',
(r'^accounts/', include('registration.urls')),
#(r'^accounts/register/$','foodies.foodapp.regview.regform'),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'static'}),
(r'^login/$', 'foodies.foodapp.regview.login'),
(r'^events/$', 'foodies.foodapp.eventview.event'),
#(r'^accounts/profile/$',
'foodies.foodapp.regview.create_profile'),
url(r'^accounts/profile/$', create_profile,
name='registration_profile'),
)
registration_form.html
-----------------------------
{% extends "base.html" %}
{% block title %}
Registration | {{ block.super }}
{% endblock %}
{% block header %}
<h1>registration_form.html</h1>
{% endblock %}
{% block content_one %}
<form action="{% url registration_register %}" method="POST">
<div class="regdiv">
<fieldset class="registration">
<legend>
Registration Details
</legend>
<table>
<tr>
<td align="right" valign="top">Username:</td>
<td>
{{ form.username }}
{% for error in form.username.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td align="right" valign="top">Email:</td>
<td>
{{ form.email }} <br/>
{% for error in form.email.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td align="right" valign="top">Password:</td>
<td>
{{ form.password1 }} <br/>
{% for error in form.password1.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td align="right" valign="top">Password (again):</td>
<td>
{{ form.password2 }} <br/>
{% for error in form.password2.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Register" /></td>
</tr>
</table>
</fieldset>
</div>
</form>
{% endblock %}
profile.html
--------------
{% extends "base.html" %}
{% block title %}
Profile | {{ block.super }}
{% endblock %}
{% block content %}
<form action="{% url registration_complete %}" method="POST">
<div class="regdiv">
<fieldset class="registration">
<legend>
Profile Details
</legend>
<table>
<tr>
<td align="right" valign="top">Username:</td>
<td>
{{ form.username }}
{% for error in form.username.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td align="right" valign="top">First Name:</td>
<td>
{{ form.first_name }} <br/>
{% for error in form.first_name.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td align="right" valign="top">Last Name:</td>
<td>
{{ form.last_name }} <br/>
{% for error in form.last_name.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td align="right" valign="top">Age:</td>
<td>
{{ form.age }} <br/>
{% for error in form.age.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Register" /></td>
</tr>
</table>
</fieldset>
</div>
</form>
{% endblock %}
the problem is when i am giving
<form action="{% url registration_profile %}" method="POST">
then the data do not save for registration auth_user table but
when i give
<form action="{% url registration_register %}" method="POST">
then i am able to save the registration details, because it call the
register function in view and save the data.
i do not know in same fashion how may i store the profile in
userprofile table.
I googled it a lot but could not get the best docs on it rather than
the others same problem
Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---