I would like to add the mugshot in django userena to the initial registration form, along with username, password and email address. I did as said in the faq (http://docs.django-userena.org/en/latest/faq.html#how-do-i-add-extra-fields-to-forms) So now I have my own form that inherits the userena signup form. I also redefined the save method to save the new mugshot in the user's profile. But now I don't know how to make that field appear in the form. I could not find any example since userena uses a modelform that automatically renders it. In the same way I can add a text field in this form as forms.CharField I would like to know what I should use to render the mugshot?
Well it appears that it is quite easy, just follow the steps described in the userena FAQ to add some extra fields to forms and in the new forms.py put something along the lines:
from django import forms
from userena.forms import SignupForm
class SignupFormExtra(SignupForm):
avatar = forms.ImageField()
def save(self):
new_user = super(SignupFormExtra, self).save()
profile = new_user.get_profile()
profile.mugshot = self.cleaned_data['avatar']
profile.save()
return new_user
Related
hello guys in django i tryed to make a simple custom user creation form extends as UserCreationForm .everything works but after save the record in database dont have password and i dont know why is that happening.
help me plz
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class MyRegistrationForm(UserCreationForm):
email=forms.EmailField(required=True)
first_name = forms.TextInput()
last_name = forms.TextInput()
class Meta:
model=User
fields=('username','email','first_name','last_name','password1','password2')
def save(self,commit=True):
user=super(UserCreationForm,self).save(commit=False)
user.email=self.cleaned_data['email']
user.first_name=self.cleaned_data['first_name']
user.last_name=self.cleaned_data['last_name']
if commit:
user.save()
return user
and in the view the code is :
def register_user_view(request):
if request.method == 'POST':
form=MyRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/success')
args={}
args.update(csrf(request))
args['form']=MyRegistrationForm().as_ul()
print(args)
return render_to_response('register.html',args)
def register_succesfull_view(request):
return render_to_response('registersucc.html')
in database
You need to put the current class, not the parent class, in the call to super:
user=super(MyRegistrationForm, self).save(commit=False)
The way you had it meant that it was skipping the parent and going straight to the grandparent, ie ModelForm, which doesn't have the custom set_password logic.
Note though that since you are using Python 3 you don't need any parameters to super at all:
user=super().save(commit=False)
which would have avoided your problem.
I am trying to implement a form wizard at the registration/signup process. I am using django-allauth for authentication and based on the docs and a previous question How to customize user profile when using django-allauth It describes how to add extra fields to the sign up form. I don't really see how I can override the default form to use a form wizard. One option I was considering is adding all the extra fields to the signup form then displaying section of the forms with ajax but I am not sure how to implement validation on the different sections. Any guidance or help on how to implement the registration step as a wizard would be greatly appreciated.
I recently did this by:
views.py
from allauth.account.forms import SignupForm
from allauth.account.utils import complete_signup
SIGNUP_FORMS = [('signup', SignupForm),
('profile', forms.UserProfileForm)]
TEMPLATES = {'signup': 'trips/forms/wizard_form.html',
'profile': 'trips/forms/wizard_form.html'}
class SignupWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self, form_list, **kwargs):
for form in form_list:
if isinstance(form, SignupForm):
user = form.save(self.request)
complete_signup(self.request, user, settings.ACCOUNT_EMAIL_VERIFICATION, settings.LOGIN_REDIRECT_URL)
elif isinstance(form, forms.UserProfileForm):
userprofile = form.save(commit=False)
user = self.request.user
userprofile.user = user
userprofile.save()
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
You can add as many forms as you want. In my case, the UserProfileForm is a ModelForm that creates a new UserProfile object with a one-to-one relationship to the User. Both objects are only saved after both forms are submitted successfully. The complete_signup function is from allauth and it does some cleanup and then logs the user in to the site.
I ended up implementing the Wizard from the client side using AngularJS Django-angular package and this library. After digging through the allauth signup view code, I figured out it already implemented an AjaxCapableProcessFormViewMixin
Implementing a wizard using client side code for the sign up process when using django-allauth is probably the best way to go since you can delay the successful redirection till all forms in the wizard are filled and also prevents splitting long signup forms into smaller forms.
I create registration form:
#urls.py
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
from account.views import Register
urlpatterns = patterns('',
url(r'^register/$', Register.as_view(template_name='account/register.html')),
)
#views.py
from django.views.generic import CreateView
from django.contrib.auth.models import User
class Register(CreateView):
model = User
success_url = '/account/'
And i have question: how I can add that email be require (now I must only enter username, password and 2 times time).
#edit
And how "repair" password? When i create user (in this form) and then go to admin panel, in user i see "Invalid password format or unknown hashing algorithm.". How i can repair this?
The reason that email is not required is because you're using a ModelForm, which takes a lot of cues from the underlying User model. Specifically, the required=True attribute is not present on the email field of the model.
One solution is to create your own form with the necessary attributes, perhaps by using a ModelForm and adding a required email field.
Another solution, and probably the better one, is to use something like django-registration as mentioned by Aamir Adnan in the comments to your question. It'll simplify things a lot for you.
As far as your repair password goes, you can't set the password to a raw string value as you're doing with your CreateView. To set a password, you have to call user.set_password(raw_string) which will take care of hashing and salting for you. Look how the built in UserCreationForm works, and try to mimic it if you decide to build the form yourself, rather than using a library (you shouldn't).
To solve these two problems you can use form like:
class UserCreationForm(forms.ModelForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
super(UserCreationForm).__init__(*args, **kwargs)
self.fields['email'].required = True
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
class Register(CreateView):
model = User
form_class = UserCreationForm
success_url = '/account/'
But there'll be other problems like duplication of username.
I have a Client model with a OneToOne relationship to User, to extend the User model.
I need to be able to register a user (with a form that includes both the User and Client fields), and for that I used django-registration. Here is the view for registration, based on some snippets I found:
def register_client(request):
if request.method == 'POST':
userform = UserForm(request.POST, prefix='user')
clientform = ClientForm(request.POST, prefix='client')
if userform.is_valid() and clientform.is_valid():
user = userform.save()
client = clientform.save(commit=False)
client.user = user
client.save()
login(request, user)
return HttpResponseRedirect('/webapp/')
else:
return HttpResponse('error!')
else:
userform = UserForm(prefix='user')
clientform = ClientForm(prefix='client')
t = loader.get_template('register.html')
c = RequestContext(request, {
'userform':userform,
'clientform':clientform,
})
return HttpResponse(t.render(c))
And here are my Forms:
from registration.forms import RegistrationForm
class UserForm(RegistrationForm):
def save(self):
new_user = User.objects.create_user(
username=self.cleaned_data['username'],
email = self.cleaned_data['email'],
password=self.cleaned_data['password1'])
new_user.backend='django.contrib.auth.backends.ModelBackend'
new_user.save()
return new_user
class ClientForm(forms.ModelForm):
class Meta:
model = Client
exclude = ['user']
I implemented the save() method for UserForm, since RegistrationForm doesn't implement one like any typical form.
Why does one have to go through all the trouble to implement some backend in order to just save a form?
Is there any problem with this simple way? It does work.
(I also added the new_user.backend='django.contrib.auth.backends.ModelBackend' so I could login the user automatically after registration.)
I'm not sure how to answer your question. If your code works then I don't see a problem. But as zaphod said, django-registration does the saving and activating of the user for you.. If you want to add extra data to your user then use django-profiles like zaphod suggested too.
I myself use django-userena. It is like django-registration and django-profiles in one.
Why do you need to save the User model? django-registration does it for you, unless you need some different functionality.
If you want to store some extra information per user, it might be better to use User profiles.
What approach is the best way to make content-types restricted to a user in Django?
Let us say I want all users with the user-role "blogger" to have its own blog.
I have created a weblog app. How do I restrict it so that the user logged in can only post in his "own" blog, and how do I make views that shows only a user's blog?
First your blog entries has to be attached to user, so you know on whos blog display, it, right? models.py:
class BlogEntry(models.Model):
user = models.ForeignKey(User, related_name='blog_entries')
other_field_1 = ...
other_field_2 = ...
Next, skip it in ModelForm, forms.py:
class BlogEntryModelForm(forms.ModelForm):
class Meta:
exclude = ('user',)
Then, when user want to post entry you require he's logged, views.py:
#login_required
def post_blog_entry(request):
....
if request.method == 'POST':
form = BlogEntryModelForm(request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.user = request.user
new_entry.save()
When you want display some user blog, views.py:
def view_blog(request, blogger_name):
user = get_object_or_404(User, username=blogger_name)
entries = user.blog_entries.all()
User is django.contrib.auth.models.User
You can add custom role checking to views above to display 404 page or error page if user has no rights to create blog.
Optionally you can replace User from django.contrib.auth with your own User implementation but you'll have to write model, authentication and middleware for it as well...
I didnt try to implement this, but I found another soultion that worked very good. It was easy to implement and did everything i wanted.
Check it out...