def userprofile(request): if request.method == "POST": form = UserProfileForm(request.POST) if form.is_valid(): form.save() return redirect('/image') form = UserProfileForm() return render(request, 'form_1.html', {'form': form})
def user_reg(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'Gigstop/user_registration.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register_user(request, role): if request.method == 'GET': context = { 'form': RegisterForm(), 'user_register_form': UserProfileForm(), 'role': role } return render(request, 'auth/register.html', context) else: register_form = RegisterForm(request.POST) if register_form.is_valid(): user = register_form.save() user_group = Group.objects.get(name=role) user_group.user_set.add(user) form = UserProfileForm(request.POST, request.FILES) if form.is_valid(): up = form.save(commit=False) up.user_id = user.id up.save() else: context = { 'form': register_form, 'role': role } return render(request, 'auth/register.html', context) login(request, user) return redirect('home') context = { 'form': register_form, 'role': role } return render(request, 'auth/register.html', context)
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(request.POST, request.FILES) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if profile_form.is_multipart(): picture = save_files(request.FILES['picture']) profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form =UserForm() profile_form = UserProfileForm() return render_to_response('app/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True new_user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, new_user) return HttpResponseRedirect('/user/') else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return JsonResponse({'errors': {'profile': profile_form.errors, 'user': user_form.errors}}, status=422);
def register(request): """ View for register returns register.html template """ if not request.user.is_authenticated(): context_dict={} if request.method == 'POST': user_form = UserRegisterForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save(commit=False) user.set_password(user.password) user.is_staff=True profile = profile_form.save(commit=False) user.save() profile.user = user profile.save() return HttpResponseRedirect(settings.REGISTER_REDIRECT_UTL) else: print user_form.errors print profile_form.errors context_dict["error1"] = user_form.errors context_dict["error2"] = user_form.errors else: user_form = UserRegisterForm() profile_form = UserProfileForm() context_dict["user_form"]=user_form context_dict["profile_form"]=profile_form return render(request, 'app/register.html',context_dict ) else : return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'app/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def profile_post(request): request.session['tab'] = 'tab1' profile = request.user.profile form = UserProfileForm(request.POST, instance=profile) if form.is_valid(): user_profile = form.save() user_profile.save() return redirect(to='profile')
def register(request): context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: pass # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render( 'app/user/register_form.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def profile(request): form_errors = None try: user_profile = request.user.userprofile except ObjectDoesNotExist: # While not strictly necessary, if there was a user created before # introducing a user profile, we will need to create the object. user_profile = UserProfile.objects.create(user=request.user) user_profile.save() """ Process the post request for form """ if request.method == 'POST': form = UserProfileForm(request.POST,instance=user_profile) if form.is_valid(): # All validation rules pass form.save() else: # Transform field name to label name and put in tuple with errors form_errors = [] for i in form.errors: try: label = form.fields[i].label if form.fields[i].label is not None else i form_errors.append((label, form.errors[i])) except: form_errors.append(('Error', form.errors[i])) """Renders the profile page.""" assert isinstance(request, HttpRequest) user_profile_form = UserProfileForm(instance=user_profile) return render( request, 'registration/profile.html', context_instance = RequestContext(request, { 'title':'Profile', 'message':'User Profile Information', 'year':datetime.now().year, 'user_profile': user_profile, 'form':user_profile_form, 'form_errors':form_errors, }) )
def profile(request): form_errors = None try: user_profile = request.user.userprofile except ObjectDoesNotExist: # While not strictly necessary, if there was a user created before # introducing a user profile, we will need to create the object. user_profile = UserProfile.objects.create(user=request.user) user_profile.save() """ Process the post request for form """ if request.method == 'POST': form = UserProfileForm(request.POST,instance=user_profile) if form.is_valid(): # All validation rules pass form.save() else: # Transform field name to label name and put in tuple with errors form_errors = [] for i in form.errors: try: label = form.fields[i].label if form.fields[i].label is not None else i form_errors.append((label, form.errors[i])) except: form_errors.append(('Error', form.errors[i])) """Renders the profile page.""" assert isinstance(request, HttpRequest) user_profile_form = UserProfileForm(instance=user_profile) return render( request, 'registration/profile.html', context_instance = RequestContext(request, { 'title':'Profile', 'message':'User Profile Information', 'year':datetime.now().year, 'user_profile': user_profile, 'form':user_profile_form, 'form_errors':form_errors, }) )
def update_user_picture(request): if request.method != 'POST': return HttpResponseBadRequest({'error': 'Only POST is accepted'}) user = get_object_or_404(UserWithProfile, user=request.user.id) profile_form = UserProfileForm(data=request.POST) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = user.user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() return JsonResponse({'message': 'Profile picture updated'}) else: return HttpResponseBadRequest({'error': 'No picture received from POST files'})
def register(request): """ Handles user registration logic Arguments: request -- [standard Django request arg] Returns: Render - render registration page with appropriate info """ registered = False if request.method == "POST": user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST, request.FILES) if (user_form.is_valid() and profile_form.is_valid()): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # check for picture if ("picture" in request.FILES): profile.picture = request.FILES["picture"] profile.save() # create wish list for new user wish_list = ProductList.objects.create(name=user.username, user=profile) logger.info("User: %s is registered", user) registered = True else: print(user_form.errors, profile_form.errors) logger.info( "User: failed to register with user form errors: %s \n \ and profile form errors: %s", user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() logger.info("Rendered registration page") return render(request, "app/register.html", context={ "user_form": user_form, "profile_form": profile_form, "registered": registered })
def add_user(request): if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() try: user.set_password(user.password) user.save() print(user) print(type(user.id)) user_id = user.id profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True try: print('Sending') request_user = AddUser(str(user_id)) client.send(request_user) print('Sent') except: print('Not sent') return redirect(reverse('login')) except: return redirect(reverse('login')) else: print(user_form.errors, profile_form.errors) return redirect(reverse('register')) else: return redirect(reverse('login'))
def register(request): registered=False user_form=UserForm(data=request.POST) profile_form=UserProfileForm(data=request.POST) if request.method=='POST': if user_form.is_valid() and profile_form.is_valid(): user=user_form.save() user.set_password(user.password) user.save() profile = profile_form.save() profile.user = user profile.save() registered=True else: print (user_form.errors, profile_form.errors) return render(request, 'app/register.html', context_instance=RequestContext(request,{'user_form': user_form, 'profile_form': profile_form, 'registered': registered} ))
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() context = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render(request, 'app/register.html', context)
def register(request): registered = False if request.method == 'POST': user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit = False) profile.user = user if 'photo' in request.FILES: profile.photo = request.FILES['photo'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() dict = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} return render(request, 'blog/register.htm', dict)
def index(request): ''' If logged in, direct to logged in user's profile page. Otherwise this is a login and registration form. ''' # Redirect to user page if already logged in if request.user.is_authenticated(): user = UserProfile.objects.get(user_id=request.user.id) return HttpResponseRedirect('/user/%d' % user.id) # Otherwise this is a registration page if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, user) user = UserProfile.objects.get(user_id=user.id) return HttpResponseRedirect('/user/%d' % user.id) else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context = {'user_form': user_form, 'profile_form': profile_form} return render(request, 'app/index.html', context)
def register(request): # A boolean value for telling the template # whether the registration was successful. # Set to False initially. Code changes value to # True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. print("registration request received") if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... print(user_form) if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, # we set commit=False. This delays saving the model # until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.name = user.username # Did the user provide a profile picture? # If so, we need to get it from the input form and #put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() password = request.POST.get('password', None) authenticated = authenticate(username=user.username, password=password) if authenticated: login(request, authenticated) # Update our variable to indicate that the template # registration was successful. registered = True else: # Invalid form or forms - mistakes or something else? # Print problems to the terminal. print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render( request, 'ClassMateZ/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })