I am trying to create UserProfile.i am using django-registration.
models.py
---------
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)
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
--------
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()
regview.py
----------
def profile_callback(self, user):
"""
Creates user profile while registering new user registration/
urls.py
"""
new_profile = UserProfile.objects.create(user=user,)
def create_profile(request, form_class='ProfileForm',
success_url=None,
template_name='registration/profile.html',
extra_context=None):
try:
profile_obj = request.user.get_profile()
print "Profile obj : ", profile_obj
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
-------
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'^profile/$', 'foodies.foodapp.regview.create_profile'),
)
when i am running http://127.0.01:8000/profile
error:
UnboundLocalError at /profile/
local variable 'form' referenced before assignment
Request Method: GET
Request URL: http://127.0.0.1:8000/profile/
Exception Type: UnboundLocalError
Exception Value:
local variable 'form' referenced before assignment
Exception Location: /home/praveen/foodies/../foodies/foodapp/
regview.py in create_profile, line 127
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---