I have changed the registration process a bit as shown below. When from the admin panel when superuser checks active, the registered user() should get mail on pressing save button. How can I do that?
Views.py
def register(request):
if request.method == "POST":
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
messages.success(request, "User saved")
return redirect("accounts:login")
else:
messages.error(request, "Error in form")
else:
form = RegistrationForm()
context = {"form": form}
return render(request, 'accounts/reg_form.html', context)
I achieved it through this link: Send an email if to a Django User if their active status is changed
Related
i want user to redirect to a from page where he has to submit detail if he is logged in site for first time or if a already a user for sometime and logged in before he will redirect to normal home page so any idea how can i achieve that in django
here is my login views
def my_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
remember_me = form.cleaned_data['remember_me']
user = authenticate(username=username, password=password)
if user:
login(request, user)
if not remember_me:
request.session.set_expiry(0)
return redirect('accounts:home')
else:
request.session.set_expiry(1209600)
return redirect('accounts:home')
else:
return redirect('accounts:login')
else:
return redirect('accounts:register')
else:
form = LoginForm()
return render(request, "login.html", {'form': form})
i want user to redirect to this form if he is logging in site for first time
#login_required
def add_address(request, username):
if request.method == 'POST':
form = Addressform(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user
form.save()
return redirect('accounts:home')
else:
form = Addressform()
return render(request, 'add_address.html', {'form': form})
other wise then to normal home page
i have also seen a same stackoverflow question where the are using signals but i dont really get how to implement in my code where session expiry is decided by user
I'm currently working on building a multi-step registration form in Django. I followed the official documentation which can be seen here. Although the forms do not show any error, the user does not get created. Is there a problem I might be overlooking?
def signup_step_one(request):
if request.user.is_authenticated:
return HttpResponseRedirect(reverse('accounts:personal-signup'))
else:
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
# collect form data in step 1
email = form.cleaned_data['email']
password = form.cleaned_data['password1']
# create a session and assign form data variables
request.session['email'] = email
request.session['password'] = password
return render(request, 'personal-signup-step-2.html', context={
'form': form,
"title": _('Create your personal account | Step 1 of 2'),
})
else:
form = CustomUserCreationForm()
return render(request, 'personal-signup-step-1.html', {
"title": _('Create your personal account | Step 1 of 2'),
'form': form,
})
def signup_step_two(request):
# create variables to hold session keys
email = request.session['email']
password = request.session['password']
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.email = email
user.set_password(password)
user.first_name(form.cleaned_data['first_name'])
user.last_name(form.cleaned_data['last_name'])
user.save()
print('user created')
return HttpResponse('New user created')
else:
form = CustomUserCreationForm()
return render(request, 'personal-signup-step-2.html', {
"title": _('Create your account | Step 2 of 2'),
'form': form,
})
I noticed that the following line in signup_step_two function will always return the message "New user created" even if form is not valid:
return HttpResponse('New user created')
Put the above line inside the if statement. Also print form.errors in else statement or inside the template to check the problem.
Ex:
if form.is_valid():
# create the user
return HttpResponse('New user created')
else:
print(form.errors)
I'm working on login/registration views in Django in a project that requires that the admin manually reviews the application before the user can actually login...while saving the information they provided at the registration page.
In the past I didn't have projects that required the users be reviewed manually by the admins.
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
new_user = form.save()
login(request, new_user)
return index(request)
else:
form = UserCreationForm()
return render(request, "registration/register.html", {"form": form})
This is what I would normally write...but even if I don't include the login(request, new_user), the new_user would be able to do so himself after the new_user is created...
I need to make sure they still can't login until the admin has manually reviewed his application to join.
Django has a semi built in way to do this. Use user.is_active, assuming that you are using the default authentication backend (you almost certainly are if you aren't sure what that is).
new_user.refresh_from_db()
new_user.is_active = False
new_user.save()
Just add these three lines after new_user = form.save()
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
new_user = form.save()
new_user.refresh_from_db()
new_user.is_active = False
new_user.save()
return HttpRequest("Thank you for signing up. Your account is pending review.")
else:
form = UserCreationForm()
return render(request, "registration/register.html", {"form": form})
Administrators can login to the Admin site and manually approve the user by changing the is_active element.
I'm new to django. I created a signup from from which data will be saved in the database at the time of login. It shows:
Please enter a correct username and password.
Note that both fields may be case-sensitive.
views.py:
def register(request):
registered = False
if request.method == 'POST':
form = signupform(request.POST,request.FILES)
if form.is_valid():
form.save()
if 'photo' in request.FILES:
form.picture = request.FILES['photo']
form.save()
return redirect("/accounts/login")
registered = True
else:
print(form.errors)
else:
form=signupform()
return render(request,'testApp/singup.html',{'registered':
registered,'form':form})
def user_login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/home/')
else:
return HttpResponse("Your 3010 account is disabled.")
else:
print("Invalid login details: {0}, {1}".format(username,password))
return HttpResponse("Invalid login details supplied.")
else:
return render(request, 'testapp/login.html', {})
To be able to manipulate your form instance, you need to "false save" your form before you make any changes to it.
You can do this via form = form.save(commit=False), that way you create a mutable instance of your form, which you can use to save your form data later after doing other manipulations or updating.
Also, you save the form instance after you have done all the manipulations.
def register(request):
registered = False
if request.method == 'POST':
form = signupform(request.POST,request.FILES)
signup_form = form.save(commit=False)
if signup_form.is_valid():
if 'photo' in request.FILES:
signup_form.picture = request.FILES['photo']
signup_form.save()
return redirect("/accounts/login")
registered = True
else:
print(form.errors)
else:
form=signupform()
return render(request,'testApp/singup.html',{'registered':
registered,'form':form})
I have a custom User model (MyUser), and a registering form (UserCreationForm) for that model. After registering the user I want it to redirect to the homepage. It is however redirecting to the homepage, but the problem is that the user is not logged in even after login() function is used in the register view, and so it is redirected back to the login page.
views.py:
#login_required(login_url='/account/login/')
def home(request):
return render(request, 'home.html')
def login_view(request):
form = LoginForm(request.POST or None)
if request.POST and form.is_valid():
user = form.login(request)
if user:
login(request, user)
return redirect("/")# Redirect to a success page.
return render(request, 'login.html', {'form': form })
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect("/")
else:
form = UserCreationForm()
return render(request, 'register.html', {
'form': form
})
Its giving me an error:
AttributeError at /account/register/
'MyUser' object has no attribute 'backend'
What am I doing wrong here? Please help me how to solve this. Thank you.
Maybe, this can solve your problem.
This will authenticate and login the user after registration.
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
password = self.request.POST.get('password', None)
authenticated = authenticate(
username=user.username,
password=password
)
if authenticated:
login(request, authenticated)
return redirect("/")
else:
form = UserCreationForm()
return render(request, 'register.html', {
'form': form
})